1

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...

6
  • 1
    You need to change the order of redirections to >>/Users/denniscook/.cronlogs/runPrinter.log 2>&1 (in both cases) if you want to capture any errors from the lp command Commented Jan 29, 2023 at 23:11
  • Have you tried to pass the full path to the lp binary? your cron has no idea about your environment and doesn't know your PATH env variable. So try that first and also to debug your cron, don't pass 2>&1 Commented Jan 29, 2023 at 23:11
  • With 2>&1 first, cron should capture the leaking output, and send it to your Linux email account: you might see them there. Commented Jan 29, 2023 at 23:21
  • @ValentinBajrami It can't hurt to do this, but man cron claims to set up PATH=/usr/bin:/bin (should find lp), along with SHELL= /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. Commented Jan 29, 2023 at 23:29
  • PROBLEM RESOLVED, thanks to your-all's help. Bottom line: cron has to be added to full disc access. The suggestion to reorder the redirect resulted in the error message "/usr/bin/lp: Operation not permitted" being written in log file. Google search "lp command in crontab on mac not permitted" resulted in a number of hits that pointed out the need for cron to be in full disc access. Thank you all for your timely responses.
    – gpaCook
    Commented Jan 31, 2023 at 21:36

1 Answer 1

0

The problem is that the cron process doesn't have full disk access.

Solution:

  • Go to Settings > Security & Privacy then select "Full Disk Access" in the list on the left.
  • Click the plus button to add a new app.
  • In the file browser type a forward slash to switch into direct entry mode. Enter the path /usr/sbin/cron. (found it by typing 'which cron' on the terminal)

Alternatively use Automator instead of cron. (If you want to learn a new tool, but that sounds annoying...)

(This answer was in the comments, and I'm just posting it here as a formal answer so the next person doesn't have to read all the comments to get the answer. Thanks for gpaCook for sharing the final solution.)

You must log in to answer this question.

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