LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-27-2015, 04:45 PM   #1
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Rep: Reputation: Disabled
Send email at completion of CUPS print job


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.
 
Old 10-27-2015, 05:22 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Have a grep in /var/log/cups for something like this:
grep "Print-Job successful-ok" /var/log/cups/*
Code:
localhost - - [22/Oct/2015:17:59:34 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 318756 Print-Job successful-ok
localhost - - [22/Oct/2015:04:21:00 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 251584 Print-Job successful-ok
localhost - - [09/Oct/2015:09:53:27 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 93552 Print-Job successful-ok
Hope that helps.
 
Old 10-27-2015, 05:36 PM   #3
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Have a grep in /var/log/cups for something like this:
grep "Print-Job successful-ok" /var/log/cups/*
Code:
localhost - - [22/Oct/2015:17:59:34 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 318756 Print-Job successful-ok
localhost - - [22/Oct/2015:04:21:00 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 251584 Print-Job successful-ok
localhost - - [09/Oct/2015:09:53:27 -0400] "POST /printers/HP_LaserJet_P2035 HTTP/1.1" 200 93552 Print-Job successful-ok
Hope that helps.
That returns no results on my system? Maybe I don't have my logs setup right?
 
Old 10-27-2015, 05:47 PM   #4
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by deerewright View Post
That returns no results on my system? Maybe I don't have my logs setup right?
It is a network printer, so this works:

Code:
grep "Send-Document successful-ok" /var/log/cups/*
That does tell me the job completed, but I want to have either CUPS or a script to send me an email at the time it completes.
 
Old 10-27-2015, 06:12 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
perhaps you should show us this cron job.

Last edited by Habitual; 10-27-2015 at 06:25 PM.
 
Old 10-27-2015, 07:15 PM   #6
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Crontab entry:

Code:
0 1 * * 2 lpr -p /path/to/file.pdf
Quote:
Windows assumes you're a dumb-ass. Linux demands that you prove it.
Have I proved it yet? :-)

Last edited by deerewright; 10-27-2015 at 07:17 PM.
 
Old 10-27-2015, 07:58 PM   #7
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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

Code:
0 1 * * 2 lpr -p /path/to/file.pdf; grep "Send-Document successful-ok" /var/log/cups/* && echo 'Success with print job' mail -s Completion email@address
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
 
1 members found this post helpful.
Old 10-27-2015, 09:09 PM   #8
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sefyir View Post
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

Code:
0 1 * * 2 lpr -p /path/to/file.pdf; grep "Send-Document successful-ok" /var/log/cups/* && echo 'Success with print job' mail -s Completion email@address
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?
 
Old 10-27-2015, 09:36 PM   #9
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
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.
 
Old 10-27-2015, 10:13 PM   #10
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks for all the help. Here is what I ended up with so far,

crontab:
Code:
0 1 * * 1 ~/weeklyprint.sh

weeklyprint.sh:
Code:
#!/bin/bash

lpr -P HP8500 /usr/share/cups/data/testprint /usr/share/cups/data/colortest.pdf

sleep 60

grep "Send-Document successful-ok" /var/log/cups/* | tail -5 >/var/log/pi/weeklyprint.log
echo "Success with print job" | mail -s "Weekly Print completed"  xxxxx@gmail.com </var/log/pi/weeklyprint.log
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
 
Old 10-27-2015, 11:04 PM   #11
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
There might be a better way, but here's a hack that might do what you want.

Code:
grep "Send-Document successful-ok" /var/log/cups/* | tail -5 >/var/log/pi/weeklyprint.log
echo "Success with print job" | mail -s "Weekly Print completed"  xxxxx@gmail.com </var/log/pi/weeklyprint.log
This doesn't check for the success of the grep command. You can check for that with $?

Eg.
Code:
~ > true
~ > echo $?
0
~ > false
~ > echo $?
1
~ > true
~ > if [ $? == 0 ]; then echo success; fi
success
As for containing both messages, this is a bit of a hack I just came up with:

Code:
echo $(echo "Success with print job"; cat /var/log/pi/weeklyprint.log) | mail -s "Weekly Print completed"  xxxxx@gmail.com
 
Old 10-27-2015, 11:18 PM   #12
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Code:
~ > true
~ > echo $?
0
~ > false
~ > echo $?
1
~ > true
~ > if [ $? == 0 ]; then echo success; fi
success
Sorry, still learning....Where do I put that code? In the script, after the grep line? Then, the following?

Quote:
As for containing both messages, this is a bit of a hack I just came up with:

Code:
echo $(echo "Success with print job"; cat /var/log/pi/weeklyprint.log) | mail -s "Weekly Print completed"  xxxxx@gmail.com
 
Old 10-28-2015, 12:03 AM   #13
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Where do I put that code?
Code:
grep "Send-Document successful-ok" /var/log/cups/* | tail -5 >/var/log/pi/weeklyprint.log
echo "Success with print job" | mail -s "Weekly Print completed"  xxxxx@gmail.com </var/log/pi/weeklyprint.log
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.
 
1 members found this post helpful.
Old 10-28-2015, 12:56 AM   #14
deerewright
LQ Newbie
 
Registered: Oct 2015
Distribution: Ubuntu, Raspian
Posts: 21

Original Poster
Rep: Reputation: Disabled
Got it! Thanks, that makes sense now...

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?
 
Old 10-28-2015, 06:00 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd try
Code:
#!/bin/bash

lpr -P HP8500 /usr/share/cups/data/testprint /usr/share/cups/data/colortest.pdf
if [[ $? -eq 0 ]]
then
    echo "Success with print job" | mail -s "Weekly Print completed"  xxxxx@gmail.com </var/log/pi/weeklyprint.log
else
    echo "Fail with print job" | mail -s "Weekly Print completed"  xxxxx@gmail.com </var/log/pi/weeklyprint.log
fi
You can tweak the msgs as reqd.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] CUPS - job completed, but no print out on the printer? pielas Linux - Hardware 12 07-11-2017 02:34 PM
seeking config "send email" when print job completed SaintDanBert Linux - Server 3 12-29-2013 11:41 AM
[SOLVED] Cups and Samba to print one print job to 2 printers - tutorial added bourne Linux - General 7 09-18-2009 10:10 AM
Clearing CUPS print job? Jonah86 Linux - Newbie 2 05-24-2004 02:11 PM
samba/cups print job size carpman Linux - Networking 1 01-25-2002 05:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration