0

Not sure what I am doing wrong, I tried running this cron job where it should run this script (test.sh) where it copies my photos from one folder to a tmp folder but the cron job does not work unless it is in my home directory (or whatever directory when you just do a 'cd') I have tried the following and it works via command line but not on cron, can someone help. This is on Mac OS with the latest sw update

*/1 * * * *  cd Desktop && ./test.sh

I've also tried the absolute path

 */1 * * * * /Users/helpme/Desktop/test.sh

My code for test.sh is below:

#!/bin/bash 
rsync /Users/helpme/Desktop/test/* /Users/helpme/Desktop/tmp/ 

When I have the following in my crontab -e

*/1 * * * * /bin/bash '/Users/helpme/Desktop/test.sh' >> /Users/helpme/Desktop/backup.log 2>&1

I get the following in my log file /bin/bash: /Users/helpme/Desktop/test.sh: Operation not permitted

Looked up the error, changed my privacy to full disk and still get the same error

UPDATE: Thank you to anonymous below in the comments for providing the article that solved the issue. Make sure to add cron to have full disk access. https://osxdaily.com/2020/04/27/fix-cron-permissions-macos-full-disk-access/

11
  • Use full paths eg: */1 * * * * /path/to/test.sh
    – Kate
    Commented May 20, 2020 at 13:45
  • I've tried that and it still does not work
    – helpme
    Commented May 20, 2020 at 16:26
  • 1
    Send the cron output to a log file. The problem should become obvious. But your script should also be using full paths. What do you think the 'current' directory is when your cronjob starts ?
    – Kate
    Commented May 20, 2020 at 17:03
  • when I open terminal and run pwd it gives /Users/helpme
    – helpme
    Commented May 20, 2020 at 17:07
  • 1
    I am not familiar with Mac but Linux in general. In cron jobs the PATH variable is typically not set so it is common to include the full path for the commands as well. So instead of rsync you may have to add its full path eg: /usr/bin/rsync. If you type which rsync you can find the command location. But based on this article it seems that the crontab executable needs additional permissions to run. Is this what you've done ? I have the impression it's crontab and not your script that is failing.
    – Kate
    Commented May 20, 2020 at 19:57

1 Answer 1

0

It sounds like your script works on the current directory instead of an absolute path. In that case the result depends on where it is located. Consider adding the code to the question so that we can help.

3
  • Yes it only works on the current directory. I'm not sure why it's doing that or how to fix it
    – helpme
    Commented May 20, 2020 at 16:27
  • that is because you use relative path's instead of absolute. Consider making the paths absolute. That is: instead of tmp/ something like /home/ruud/tmp (absolute paths starts from the root, so with a slash). Only tmp/ means: the subdirectory tmp of the current directory. That is why the location where you start the script matters.
    – ruud
    Commented May 20, 2020 at 18:02
  • I tried that and it still does not seem to work, see edits for test.sh
    – helpme
    Commented May 20, 2020 at 18:14

You must log in to answer this question.

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