SOLVED - Using cron to print text files?? Possible?

truckerDave

Active Member
Joined
Oct 7, 2023
Messages
210
Reaction score
191
Credits
1,798
I have a php page that takes a 30ish line of text and pulls relevant information and enters it into my database. I want to be able to automatically print the original text entered.

I read an old post on one of the coding sites that talked about a similar situation. The poster wanted to automatically print files added to a certain folder. The folks over there mentioned setting up a cron job to do what he wanted.

I have 0 experience with anything cron. Is this a possibility for my case? I have no issues writing the text to a file and saving it to a folder if that's what it takes.

If this is a solid method, any advice on a cron program? I'm "assuming" there's more than one available (I'm using Ubuntu, BTW). And I'll take any advice on links to information, tutorials, etc.

Thanks everyone!
 


Print? To a printer? What kind of files? Text? PDF? Graphics? HTML?

lp filename.txt

lp filename.pdf

crontab
0 * * * * /usr/bin/lp /path/to/my file

That will print that file every hour.
If there are multiple files with multiple names, then you will have to add some logic or looping.
You could put it all in a bash script, and then run the bash script however often is needed.
 
Print? To a printer? What kind of files? Text? PDF? Graphics? HTML?

lp filename.txt

lp filename.pdf

crontab
0 * * * * /usr/bin/lp /path/to/my file

That will print that file every hour.
If there are multiple files with multiple names, then you will have to add some logic or looping.
You could put it all in a bash script, and then run the bash script however often is needed.
Yes. Trying to have my server send a plain text file to a printer automatically after the html form is submitted by a client machine.

I've been reading up on the subject and I'm starting to think "cups" may be the answer. As I'm not sure at this time, if "scheduling" will cause an issue or not. And each time the job is ran it will possibly be a different file name. Still unsure though.
 
Shouldn't that be:
0 1 * * * /usr/bin/lp /path/to/my file

Because I have /etc/crontab open right now in a text editor (Spiral Linux KDE with "Bullseye" base and kernel v6.1.0-deb11.11) and it has "help" there suggesting that minutes field is first, followed by hour and then three more fields and then the user to run the command and finally the command to run. (shrugs)

In fact, there are three commands which purposely have a tab separating the minutes and hours from the rest of the line entry. Like this:

Code:
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *    root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *    root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

(This "code" thing doesn't show well where the tabs are.)
 
Shouldn't that be:
0 1 * * * /usr/bin/lp /path/to/my file
No, what you're showing would execute the command at 1AM (hours designated as 0-23)... and at exactly 1AM (the 0 minute of the hour). If you showed * 1 * * * the command would execute every minute from 1:00 - 1:59 AM. If you want the command to execute at 1:15 AM every day, then 14 1 * * * (remember to count from zero). 1:15 PM = 14 13 * * *.

@dos2unix showing 0 * * * * executes the command at the 0 minute of every hour, or in other words, specifically at "the top of the hour" of every hour.

However, I cannot get the command /usr/bin/lp <path/to/textfile> to actually print to my printer due to a "No default destination" error (using Linux Mint). I hope the OP has better luck if trying to use cron like this. Another way that may work is to put the print commands inside a script, and then call the script from cron. I would still have trouble because I can't get lp or lpr to work from the command line at all. That's where I see the error from both commands.
 
No, what you're showing would execute the command at 1AM (hours designated as 0-23)... and at exactly 1AM (the 0 minute of the hour). If you showed * 1 * * * the command would execute every minute from 1:00 - 1:59 AM. If you want the command to execute at 1:15 AM every day, then 14 1 * * * (remember to count from zero). 1:15 PM = 14 13 * * *.

@dos2unix showing 0 * * * * executes the command at the 0 minute of every hour, or in other words, specifically at "the top of the hour" of every hour.

However, I cannot get the command /usr/bin/lp <path/to/textfile> to actually print to my printer due to a "No default destination" error (using Linux Mint). I hope the OP has better luck if trying to use cron like this. Another way that may work is to put the print commands inside a script, and then call the script from cron. I would still have trouble because I can't get lp or lpr to work from the command line at all. That's where I see the error from both commands.
I have something like this already set up and been working for a long time. It executes every 10 minutes and below is the script I use.

#!bin/bash
rm /mnt/LISS/Info/PrintMe/process/.
mv /mnt/LISS/Info/PrintMe/. /mnt/LISS/Info/PrintMe/Process
lpr -P MX920-series /mnt/LISS/Info/PrintMe/Process/.
done

I move all files to a different folder then process it. I do this on the off chance somebody adds a file during the process I don't want it lost. It also ensures only 1 print per item. the trick with lpr is having the exact name of the printer
 
I hope the OP has better luck if trying to use cron like this.
Thanks fellas for the replies.

Luck is something I have never had. So, I really don't think cron is going to work in my case. After some careful thought and further reading, I think I need a method that works somewhat instantly with the form submission. I'm looking into a couple of php libraries that add that ability to php utilizing cups. For now, the entire system is in-house and not exposed to the outside world. Eventually, I want to be able to access it from my phone when on the road. So, I'm also looking into the security aspect of my final setup.

As I've said before, I'm a complete newb with most of this stuff. But I'm not afraid to learn when I need to. I really do appreciate you folks sharing your knowledge.
 
However, I cannot get the command /usr/bin/lp <path/to/textfile> to actually print to my printer due to a "No default destination" error (using Linux Mint).

I was assuming he was already able to print to a printer. If you have GUI, you can setup a printer. If you only have a command line, you will have to install cups and run lpadmin.
 
I was assuming he was already able to print to a printer. If you have GUI, you can setup a printer. If you only have a command line, you will have to install cups and run lpadmin.
The lpr command is in the package: cups-bsd:

Code:
[tom@min ~]$ apt-file list cups-bsd
cups-bsd: /usr/bin/lpq                   
cups-bsd: /usr/bin/lpr
cups-bsd: /usr/bin/lprm
cups-bsd: /usr/sbin/lpc
cups-bsd: /usr/share/doc/cups-bsd
<snip>

Works fine here.

It's also in the packages: lpr, and, lprng.
 
I think I need a method that works somewhat instantly with the form submission.
That's a great idea, if you can get your php script to spit out the data to a printer at the same time as posting to your database. Another chance might be for the database to do the work and take this input and send it to the printer, like with a macro.

If the php script can spit out the data to a text file more easily, then cron still may work for you too. With @APTI's hint above, I got cron to print with both lpr and lp from the command line. Here is the syntax I entered into my crontab:
Code:
0 * * * * /usr/bin/lpr /home/atanere/mytextfile.txt -P HP_OfficeJet_Pro_6970_068988_USB

lp -d HP_OfficeJet_Pro_6970_068988_USB /home/atanere/mytextfile.txt worked in a terminal, so it would probably work in cron also.

Yes, gotta have a printer name! And you can tell in my example why I didn't remember that!

A couple of ways to find the printer name are:
Code:
lpstat -a

# or

cat /etc/printcap

Of course, in this example it will help to make the textfile name the same every time you generate new data. And you need a cron schedule that suits your needs, but cron is pretty flexible.

Whatever you decide, good luck!
 
Last edited:
That's a great idea, if you can get your php script to spit out the data to a printer at the same time as posting to your database. Another chance might be for the database to do the work and take this input and send it to the printer, like with a macro.

If the php script can spit out the data to a text file more easily, then cron still may work for you too. With @APTI's hint above, I got cron to print with both lpr and lp from the command line. Here is the syntax I entered into my crontab:
Code:
0 * * * * /usr/bin/lpr /home/atanere/mytextfile.txt -P HP_OfficeJet_Pro_6970_068988_USB

lp -d HP_OfficeJet_Pro_6970_068988_USB /home/atanere/mytextfile.txt worked in a terminal, so it would probably work in cron also.

Yes, gotta have a printer name! And you can tell in my example why I didn't remember that!

A couple of ways to find the printer name are:
Code:
lpstat -a

# or

cat /etc/printcap

Of course, in this example it will help to make the textfile name the same every time you generate new data. And you need a cron schedule that suits your needs, but cron is pretty flexible.

Whatever you decide, good luck!
Thank you!

I think I hit bottom with the php route. Seems none of the libraries are being updated for the moment. One library is still being developed but is still not stable. Oh well.

One issue I am stuck on with using cron though. This whole scheduling thing. If I'm thinking about this correctly, if I set up a schedule, it's going to print on that schedule and that schedule only. Regardless of what text is in the file. It has no way of knowing when/if the file has changed. Just that it is time to print. Thus, if I'm on the road for 2 or 3 weeks, I could come home to a pile of the same copies and no printer ink. If I start the cron to run when I get home, it's only going to print the last entry I made even though I may have made 10 or 12.

In my OP above, I mentioned reading about a fellow who was doing something similar. Well, I thought about that some more and even searched for the website again (to no avail). However, I seem to remember them talking, not about cron per se, but about incrond and the ability to monitor a file and act when it changed. Something to that effect would be perfect!
 
Thank you!

I think I hit bottom with the php route. Seems none of the libraries are being updated for the moment. One library is still being developed but is still not stable. Oh well.

One issue I am stuck on with using cron though. This whole scheduling thing. If I'm thinking about this correctly, if I set up a schedule, it's going to print on that schedule and that schedule only. Regardless of what text is in the file. It has no way of knowing when/if the file has changed. Just that it is time to print. Thus, if I'm on the road for 2 or 3 weeks, I could come home to a pile of the same copies and no printer ink. If I start the cron to run when I get home, it's only going to print the last entry I made even though I may have made 10 or 12.

In my OP above, I mentioned reading about a fellow who was doing something similar. Well, I thought about that some more and even searched for the website again (to no avail). However, I seem to remember them talking, not about cron per se, but about incrond and the ability to monitor a file and act when it changed. Something to that effect would be perfect!
if you look further up you will see how I wrote the script to print everything in the folder and only do it once. Just copy the script and adjust for the directory you want to print and the printer to send to. I promise you will only get one copy of each that way.

You could even modify it so that you keep an archive of it by just moving the files to an archive directory instead of deleting. Just set the script to run using chron at the time intervals you want. if you have issues try installing webmin and it has a very easy GUI for chron jobs.

maybe I should mention that you do have to make sure the directories already exist before running.
 
maybe I should mention that you do have to make sure the directories already exist before running.
With me, it's never a bad idea to mention what might be obvious to others!

I'll have to play around with what you showed above. Might be the cure. Thanks again.
 
However, I seem to remember them talking, not about cron per se, but about incrond and the ability to monitor a file and act when it changed. Something to that effect would be perfect!
Sure, that should work too. You can read up on that tool here. :cool:

Or continue looking at @APTI's method. :cool:

There are probably a half dozen other ways to achieve your goal... gotta love Linux. :cool:
 
Here's what I have come up with for now .... With an issue maybe someone can help with.

I installed Incron and for testing purposes ...

Created the file /usr/action.sh that includes the following
Code:
lp /var/www/html/2023/inc/print/print.txt

then did the command incrontab -e and inserted the following
Code:
/var/www/html/2023/inc/print/print.txt IN_MODIFY /usr/action.sh

This works. Although, with an issue. I modified the txt file 3 different times. The first 2 times it printed perfectly. The third time it printed 3 copies. I can't see this being an issue with my coding. But what do I know? Anyone know why it would print multiple copies? Or what I might do to keep that from happening? Hate to waste the most expensive fluid on the planet (or maybe 2nd most expensive).

EDIT 1:
Fourth test ran 2 copies :mad:

EDIT 2:
I added a line to the action.sh file that logs the time an event occurs
Code:
#!/bin/bash


echo "File modified on $(date +'%d-%m-%y') at $(date +'%T')" >> /home/dave/Desktop/log.txt


lp -n 1  /var/www/html/2023/inc/print/print.txt
When I get multiple copies, I get multiple entries.
Code:
File modified on 08-12-23 at 07:05:42              <-------Just one copy
File modified on 08-12-23 at 07:06:38              <-------First of 2
File modified on 08-12-23 at 07:06:38              <-------Second of 2
So, now I need to figure out how to get incrond to only flag it one time.
 
Last edited:
OK, I have it working (for now). I will update if it all goes south later on. In case someone stumbles across this thread having the same issue, what is working is changing the incrontab file from
Code:
/var/www/html/2023/inc/print/print.txt IN_MODIFY /usr/action.sh
to
Code:
/var/www/html/2023/inc/print/print.txt IN_CLOSE_WRITE /usr/action.sh
based on the following
  • Q: What is the difference between IN_MODIFY and IN_CLOSE_WRITE?
    The IN_MODIFY event is emitted on a file content change (e.g. via the write() syscall) while IN_CLOSE_WRITE occurs on closing the changed file. It means each change operation causes one IN_MODIFY event (it may occur many times during manipulations with an open file) whereas IN_CLOSE_WRITE is emitted only once (on closing the file).
That information came from here.

Thanks everyone!
Now off to re-learn how to write php code CORRECTLY and to today's standards. Have to update my 10 year old code. Vulnerabilities galore!
 


Latest posts

Top