Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am running a headless raspberry pi for several functions, and have a cron job set to print a test page weekly to a network printer. I would like to have an email sent to confirm that the job has completed successfully. I know how to send an email about the cron job, but that only tells me the cron completed, not whether or not CUPS printed successfully or not.
You could either have the cronjob use a one-liner to do everything, or invoke a small bash script.
The former can be finicky but wouldn't require creating a script file while the latter would be simpler.
For the one-liner I suppose something like this may work
You could either have the cronjob use a one-liner to do everything, or invoke a small bash script.
The former can be finicky but wouldn't require creating a script file while the latter would be simpler.
For the one-liner I suppose something like this may work
I'd be careful of cron interpreting the * and @ weirdly
Out of curiosity, if you run lpr -p /path/to/file.pdf, will it have a error code if it fails? Because you could skip the grep
Code:
0 1 * * 2 lpr -p /path/to/file.pdf && echo 'Success with print job' | mail -s Completion email@address
I used your second example, and it works. I do not get any error messages, but I kept getting my syntax wrong, and I would get a strange formatted email that basically repeated the cron command rather than 'Success..., etc.'
Once I got all my syntax right, it works fine. I just not sure if it is really telling me it printed or just that the cron job completed? Maybe I'll try to write a script that does the print and grep, and email, then cron the script?
I just not sure if it is really telling me it printed or just that the cron job completed? Maybe I'll try to write a script that does the print and grep, and email, then cron the script?
&& means if the previous command succeeds, also do this command.
Eg, try these two commands
Code:
true && echo success
false && echo success
You'll notice when the command fails (with false), echo does not stdout Success.
This seems to give me what I'm looking for, but just for my own edumacation, anything I should change?
Also, It does not print "Success with Print job" in the body of the email, just the log file. Is there a way to get both, without using an attachment? Can you send multiple files in the body?
Last edited by deerewright; 10-27-2015 at 10:39 PM.
Reason: added info
I'm assuming your intended purpose of the two lines above is to search for "Send-Document successful-ok" with grep and if that is successful, to send a email saying "Success with print job" as the message.
Now, if it is not a success, what should the action be?
As the code is, no matter what it will send a "Success with print job" everytime it is executed since it never evaluates the result of the grep search.
This would be the basis of a if then statement.
Something like this:
Code:
grep "Send-Document successful-ok" /var/log/cups/* | tail -5 >/var/log/pi/weeklyprint.log
if [ $? == 0 ]
then
echo "Success with print job" | mail -s "Weekly Print completed" xxxxx@gmail.com </var/log/pi/weeklyprint.log
else
command to give if stuff fails
fi
Or simplified as a one-liner
Code:
grep "Send-Document successful-ok" /var/log/cups/* | tail -5 >/var/log/pi/weeklyprint.log
if [ $? == 0 ]; then echo "Success with print job" | mail -s "Weekly Print completed" xxxxx@gmail.com </var/log/pi/weeklyprint.log; fi
Assuming you understand what a if statement is meant for, $? is used to evaluate the error code of the previous command. Apologies if I didn't explain that well enough with my previous example. 0 is meant for no error, 1 is for error and other various numbers are used for specialized errors.
Since you're looking to send a email when the command resolves successfully, the if statement tests for $? equalling 0. If it does, it sends the email saying success. Otherwise, it does nothing. If you place commands in the command to give if stuff fails spot, it will run those commands if the if statement doesn't match.
BUT, I just realized that it will send a successful print message as long as it finds any "Send-Document successful-ok" messages in the log. So we need to find a way to limit it to the most recent (i.e. within the last few minutes), ideally.
I guess I could delete the log file before printing?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.