2

I have a remote Mac that is connected to the printer.

I trigger the print on my Mac remotely using ssh from my local system using "ssh port forwarding/tunneling" discussed here.

Below is my code on the local system that triggers the printer lp command on the remote server and checks if it was successfully printed.

until ssh -p 3334 remuser@localhost "lp -d Brother_HL_L2350DW_series $HOMEDIR/Printed/$NEWFILE" >/home/system/send4print/printererror.log 2>&1

do

echo "Exit Code of the command was: $?"
echo "Send email that there is an issue printing invoice for the below file. Issue is: `cat /home/system/send4print/printererror.log`"
ls -ltr $FILE >>/home/system/send4print/printererror.log

mail -s "PRINTER SERVICE FAIL ALERT. PLEASE CHECK YOUR PRINTER!!" [email protected] < /home/system/send4print/mailbody.txt

sleep 20

done

echo "Print successful. Deleting $FILE"

I got the below message printed in the logs indicating that the print was successful.

nohup.out:Print successful. Deleting /home/system/send4print/online_delivery_10000656.pdf

However, upon checking the remote Mac OS system printing had failed due to the below error.

online_delivery_10000656.pdf
Stopped - Can't open "/private/var/spool/cups/d12637-001"

The snapshot of the same is attached herewith.

I need help with a couple of stuff here:

  1. In case of such failures like Can't open "/private/var/spool/cups/d12637-001" or others on the remote host; I want the return code on in my local script as non-successful while currently, I get 0

  2. Remedy for this error Can't open "/private/var/spool/cups/d12637-001" so that I can retry the print and make sure it gets printed the second time.

1
  • See my answer in case it work for you :) Commented Mar 27, 2021 at 22:45

1 Answer 1

0

Catching exit code right

First, let's solve the problem with the way you handle exit code:

ssh -p 3334 remuser@localhost "lp -d Brother_HL_L2350DW_series $HOMEDIR/Printed/$NEWFILE" >/home/system/send4print/printererror.log

SERVICE_EXIT_STATUS=$? # store the exit code

# mail if failed
if [ $SERVICE_EXIT_STATUS -ne 0 ];then
    mail -s "PRINTER SERVICE FAIL ALERT. PLEASE CHECK YOUR PRINTER!!" [email protected] << /home/system/send4print/mailbody.txt # email when there is an error...
    echo "$FILE is the culprit" >>/home/system/send4print/printererror.log # adding at end of file like in your original script the filename of the "culprit"
    echo "Send email that there is an issue printing invoice for the below file. Issue is: `cat /home/system/send4print/printererror.log`" # printing like your original script the log
else
    echo "Print successful. Deleting $FILE"
    #this is where you may delete the file i guess?
fi;

Using until as requested:

until ssh -p 3334 remuser@localhost "lp -d Brother_HL_L2350DW_series $HOMEDIR/Printed/$NEWFILE" >/home/system/send4print/printererror.log &> /dev/null

SERVICE_EXIT_STATUS=$? # store the exit code

do
# mail if failed
if [ $SERVICE_EXIT_STATUS -ne 0 ];then
    mail -s "PRINTER SERVICE FAIL ALERT. PLEASE CHECK YOUR PRINTER!!" [email protected] << /home/system/send4print/mailbody.txt # email when there is an error...
    echo "$FILE is the culprit" >>/home/system/send4print/printererror.log # adding at end of file like in your original script the filename of the "culprit"
    echo "Send email that there is an issue printing invoice for the below file. Issue is: `cat /home/system/send4print/printererror.log`" # printing like your original script the log
else
    echo "Print successful. Deleting $FILE"
    #this is where you may delete the file i guess?
fi;

done

This should work. Though i don't have a printer on my end, i did test if the logic worked (it does as far as i know).

About your printer error

I don't have a printer nor your printer's brand BUT, just by searching around, i found a couple of post with similar error to yours, some on Mac (like you mentioned you were on), and other one:

etc

Essentially, there is four possible fix/reason why you got this error:

  • The version of Adobe (which is what you might use on your end when printing, or maybe your printer use it since it's a pdf) has a bug or is too old for whatever reason, maybe updating it might work?
  • You could use the setting in your printer/or on your mac to print as "image" instead, although that would work, I'm unsure if that's what you want.
  • You could use a different program to print, maybe one of the myriad of open source implementation that may or may not depend on the Adobe api to print your pdf, in which case it could work (don't have a Mac so i believe it would be better to ask about this on the apple centric SE).
  • It might be something else on either Apple side or your printer's brand side. See this post for an example of what i mean.
6
  • 1
    The issue of successful exit code although the print failed can only be tested with the until loop which your answer does not consider. Can you update my code the corrections/suggestions you have? @Nordine Lotfi
    – Ashar
    Commented Mar 28, 2021 at 18:59
  • oh, sure thing. Will edit in a bit :) @Ashar Commented Mar 29, 2021 at 18:20
  • Wait, if I'm not wrong, the above code works fine to catch the error if the print fail...though i sadly can't fully test it (beyond testing the initial logic) since i don't have a printer at hand right now...Does it really fail to catch the error? @Ashar Commented Mar 29, 2021 at 18:28
  • The until loop essentially isn't really needed here if you need "finer control" over what you want the script to do in case it fail (which it take into account here)...It'll be akin to fail infinitely, which is kind of obscure method to use for printer/printing document. @Ashar Commented Mar 29, 2021 at 18:35
  • this issue is intermittent hence any proposed solution can only be tested using until loop.
    – Ashar
    Commented Mar 29, 2021 at 22:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .