On my Macos 11.6.1, I want to execute a script 'task1.zsh' with the following content
#!/bin/zsh
/usr/bin/touch /Users/user/Documents/cronjobs/hello.txt
echo hello >> /Users/user/Documents/cronjobs/hello.txt
the file task1.zsh
has the following permissions:
-rwxr-x--x 1 user staff 116B Jun 18 00:15 task1.zsh*
I inserted the following task with cronjob (with the following output of the command crontab -l
:
* * * * * /Users/user/Documents/cronjobs/task1.zsh
with this cronjob, however the file "hello.txt" is not created nor populated. I also tried to replace
* * * * * /Users/user/Documents/cronjobs/task1.zsh
by:
* * * * * cd /Users/user/Documents/cronjobs/ && ./task1.zsh
as well as
* * * * * /bin/zsh /Users/user/Documents/cronjobs/task1.zsh
or
* * * * * cd /Users/user/Documents/cronjobs /bin/zsh task1.zsh
without success.
However If I had the following other task to the cronjob list:
* * * * * cd /Users/user/Documents/cronjobs && /bin/zsh task1.zsh
* * * * * cd /Users/user/Documents/cronjobs && echo check >> cron1.log
the file cron1.log
gets created and gets populated by the string "check" (but the file hello.txt
does not get created nor populated)
What am I doing wrong?
N.B. I originally asked the question on the main stackoverflow website (https://stackoverflow.com/questions/76499197/task-scheduled-as-a-cronjob-not-executing?noredirect=1#comment134921903_76499197) and I was advised to post the question on Ask Different.
EDIT 1
I replaced :
* * * * * cd /Users/user/Documents/cronjobs && /bin/zsh task1.zsh
* * * * * cd /Users/user/Documents/cronjobs && echo check >> cron1.log
* * * * * cd /Users/user/Documents/cronjobs && /bin/zsh task1.zsh
* * * * * echo “Hello” >> /Users/user/Documents/cronjobs/hello.txt
and indeed it worked
SOLUTION
As mentionned in the comment I went to Security and Privacy and granted full disk access to cron and it worked according to Crontab Operation not permitted
* * * * * echo “Hello” >> /Users/user/Documents/cronjobs/hello.txt
. That will skip creating a new subshell to run the script. Does it work? Also, is there a reason you’re not using launchd? – Allan Jun 21 '23 at 17:12and indeed it worked
– ecjb Jun 21 '23 at 17:21