简体   繁体   中英

Running script with cron doesn't work on mac

I keep trying to run a cron job that executes a python script every minute. Having executed "which python", I set up the cron job as follows:

SHELL=/bin/bash
MAILTO=MyMac

PATH=bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

*/1 * * * * * /Users/MyMac/anaconda3/bin/python
/Users/MyMac/desktop/cron_test/test.py

Job's description is in one line and there is a new line character at the end of the definition.

I get the following error in /var/mail/MyMac :

/bin/bash: AnacondaProjects: command not found

So I deleted:

SHELL=/bin/bash

and I got:

/bin/sh: MyMac: command not found

Then I tried all possible combinations of /usr/bin/python with or without lib, anaconda etc., with or without specifying PATH, SHELL, MAIL. Unfortunately, without success.

What am I doing wrong?

Edit

So here's the summary of what I did according to the pieces of advice I received:

I tried:

* * * * * env > /tmp/env.output, 

first I got an error:

/bin/bash: /tmp/env.output: Permission denied, 

so I made a cron Job as sudo. The path in the env.output is:

PATH= bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/Users/MyMac/AnacondaProjects

Finally I set my cronjob (as a normal user not as sudo) to:

SHELL=/bin/bash
MAILTO=my_address@mail.com
PATH=bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/Users/MyMac/AnacondaProjects:/Users/MyMac/anaconda3/bin/python:/usr/bin/env

 * * * * * /Users/MyMac/anaconda3/bin/python /Users/MyMac/desktop/cron_test/test.py

It still doesn't work. The python code is:

#!/usr/bin/env python
def main():
    f = open("test.txt", "w+")
    f.write("HELLO" + '\n')
    f.close()


if __name__ == "__main__":
    print("Print")
    main()

I'm looking forward forward to get and try out new approaches.

Try doing this:

  1. Open preferences and go to “Security & Privacy” -> “Privacy”
  2. Scroll down to “Full Disk Access” on the left side and select it.
  3. Hit the unlock icon and enter your password
  4. Hit the “+” sign to add an application
  5. Type “command+shift+G” to bring up the got to file dialog box (don't seem able to navigate the /usr directory)
  6. Enter the full path name to the application (/usr/sbin/cron) and click on “Go”
  7. Click “Open” It should show up in the list with a check mark next to it. I tried it with a simple csh script in cron and it worked. (Credit to my source: https://blog.bejarano.io/fixing-cron-jobs-in-mojave )

Try doing the following:

  • In your python's script first line put: #!/Users/MyMac/anaconda3/bin/python
  • Make sure your script is executable: chmod +x /Users/MyMac/desktop/cron_test/test.py
  • Change the cron recipe to: * * * * * * /Users/MyMac/desktop/cron_test/test.py which is a form of telling cron to execute the job every minute

Try to make cron record like this:

* * * * * /Users/MyMac/anaconda3/bin/python /Users/MyMac/desktop/cron_test/test.py

This will run it every minute

And on MAILTO cron expect email address, not some name

All existing answers to this question (while helpful general cron suggestions) are wrong.

OP error is a cron with six time entries, not five:

*/1 * * * * * /Users/MyMac/anaconda3/bin/python

Therefore cron reads the first five entries as time instructions and the sixth * as a terminal instruction (a wildcard that expands to a list of files/directories in the current directory), and then tries the first string in that list as a command and gives an error.

This will typically give errors like:

/bin/bash: Applications: command not found
/bin/bash: AnacondaProjects: command not found
/bin/zh: Applications: command not found
etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM