I need to run this lp command periodically:
lp /Users/denniscook/Documents/PrinterManagement/UseInk.pdf 2>&1 >>/Users/denniscook/.cronlogs/runPrinter.log
I have this command in runPrinter.zsh script:
#!/usr/bin/env zsh
cat >>/Users/denniscook/.cronlogs/runPrinter.log <<EOF
***************************
Enter Job `date`
EOF
lp /Users/denniscook/Documents/PrinterManagement/UseInk.pdf 2>&1 >>/Users/denniscook/.cronlogs/runPrinter.log
cat >> /Users/denniscook/.cronlogs/runPrinter.log <<EOF
Exit Job `date`
EOF
The lp command is executed correctly when directly issued in terminal, or when the script is issued in terminal- meaning printer prints and log gets updated. But when either the command or the script is run from cron, neither works.
In crontab, I have tried two approaches using the crontab below, commenting out one entry and using the other. The first will run the script, the second attempts to run the command directly.
#-----------#
# cron jobs #
#-----------#
#06 11 * * * /Users/denniscook/.scripts/runPrinter.zsh
13 11 * * * lp /Users/denniscook/Documents/PrinterManagement/UseInk.pdf 2>&1 >>/Users/denniscook/.cronlogs/runPrinter.log
~
~
When script is executed from terminal, and it works correctly, log entry is:
***************************
Enter Job Sun Jan 29 11:07:26 HST 2023
request id is EPSON_WorkForce_845-1286 (1 file(s))
Exit Job Sun Jan 29 11:07:26 HST 2023
When cron executes the script, the log entry is as follows (no output from lp cmd):
***************************
Enter Job Sun Jan 29 12:15:00 HST 2023
Exit Job Sun Jan 29 12:15:00 HST 2023
When cron executes lp command directly,there is no log entry.
I don't know what difference there is between cron executing the job, and my executing them in terminal. My research failed to find an answer.
I appreciate any help...
>>/Users/denniscook/.cronlogs/runPrinter.log 2>&1
(in both cases) if you want to capture any errors from the lp commandlp
binary? your cron has no idea about your environment and doesn't know yourPATH
env variable. So try that first and also to debug your cron, don't pass2>&1
2>&1
first, cron should capture the leaking output, and send it to your Linux email account: you might see them there.man cron
claims to set upPATH=/usr/bin:/bin
(should findlp
), along withSHELL= /bin/sh
, LOGNAME and HOME (both taken from/etc/passwd
). Granted, "it works in terminal but not on cron" is a .profile problem 70% of the time.