0

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

ecjb
  • 514
  • 1
  • 3
  • 14
  • Start diagnosing by changing the line in your crontab to be * * * * * 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:12
  • Many thanks for your comment @Allan. As I use Linux in parallel I want to use code which works for both linux and macos. 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

    – ecjb Jun 21 '23 at 17:21
  • Then we know cron is working. After looking at your permissions again, try setting the permissions to read and execute for everyone. – Allan Jun 21 '23 at 17:34
  • Users' Desktop folders (and Documents and many other places) have special privacy protection in addition to regular file permissions, and you may be running into trouble with that. See "What and how does macOS Mojave implement to restrict applications access to personal data?", "Crontab Operation not permitted", 'Why do I get "ls: Desktop: Operation not permitted" when I own Desktop?', etc. – Gordon Davisson Jun 21 '23 at 19:00
  • ok thank you @Allan for your comment. I tried it but unfortunately it didn't work – ecjb Jun 21 '23 at 21:25
  • thank you @GordonDavisson. Indeed, I went to Security and Privacy and granted full disk access to cron and it worked – ecjb Jun 21 '23 at 21:27
  • @ecjb I'm glad you got it working, but I'd recommend against just turning off protection for cron jobs. It'd be best to move the script & its files to a non-private location, and the second-best option would be to give cron access to just your Documents folder (in the Files and Folders section) rather than full disk access. – Gordon Davisson Jun 22 '23 at 00:23
  • many thanks for your comment @GordonDavisson. What would typically be a "non-private" location on macos? – ecjb Jun 22 '23 at 04:43
  • @ecjb I don't have a current version of macOS handy for testing, but I think the main locations that're restricted are the Desktop, Documents, and Downloads folders, plus several things under ~/Library. Creating a new top-level folder in your home directory might be the simplest solution. – Gordon Davisson Jun 22 '23 at 05:10

0 Answers0