1

I'm running macoS Ventura (13.1). Bash is my default shell. Please bear with me - I am still a shell noob.

I have a shell script stored in a file on my system. I can successfully run the shell script from the Terminal: sh ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/cronjob_test.sh

After several unsuccessful runs I have stripped out some of the code to simplify it - still no joy though. Here is the simplified script:

#! /bin/bash

DEVICE_NAME="MBP "

# Get battery percent charge
BATTERY_PERCENT=55

# Append to log
timestamp() {
  date +"%Y-%m-%d %H:%M:%S"
}
TIMESTAMP=$(timestamp)
echo "$TIMESTAMP,$DEVICE_NAME,$BATTERY_PERCENT,Cronjob Test" >> ~/crontab_log.csv

exit 0

A new csv row is added to crontab_log.csv when I run the script as above. But when I try to call this script from crontab I get an error, this is from mail: bin/bash: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted

If you are able to help me fix this it would be awesome! Thanks for reading.

Below are my unsuccessful attempts to call the script from crontab (every 4 hours):

0 */4 * * * bash ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * bin/bash ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * sh ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh

I was hoping that the script would be executed and a new row of data added to my csv file.

Per this question [https://stackoverflow.com/questions/31217026/shell-script-my-crontab-script-does-not-run] I have also tried:

  • chmod +x ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/cronjob_test.sh
  • changing script to just this :-
#! /bin/bash

echo "running"

Still receive same error: sh: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted

Dan
  • 43
  • 7
  • 1
    I have a feeling this is due to MacOS sandboxing. Something needs full disk access, but I'm not sure what. [apple.se] would probably be a better place to post this. – Barmar Jan 18 '23 at 22:42
  • 2
    Users' Desktop folders (and a number of other subfolders of the home directory) have additional privacy protection, besides the usual file permissions. You probably need to either move the script (and any files it uses) someplace not considered private, or grant `cron` access to your Documents folder. See [this](https://apple.stackexchange.com/questions/332673), [this](https://stackoverflow.com/questions/73256814), and [this](https://apple.stackexchange.com/questions/378553). – Gordon Davisson Jan 18 '23 at 23:16
  • Thanks so much Barmar and Gordon this is exactly a permissions issue. I have granted cron full disk access and my modified and original cronjobs are now running!! I'm so happy. – Dan Jan 19 '23 at 20:33

1 Answers1

2

Just to close this question out I am adding the correct solution which came from the comments from Barmar and Gordon. The issue I was facing was a Cron permissions issue - I wasn't able to deduce that from the error messages.

Per the answer to this question I gave full disk permission to Cron and my cronjobs now work as expected.

Dan
  • 43
  • 7