7

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.

SkogensKonung
  • 601
  • 1
  • 9
  • 22
  • Looks like your script cannot find `AnacondaProjects` in the path specified (cron will not take over your user's path ). – user2314737 Feb 01 '19 at 22:25
  • I added /Users/MyMac/AnacondaProjects to the PATH, but it doesn't work. – SkogensKonung Feb 01 '19 at 22:35
  • Is `/Users/MyMac/desktop/cron_test/test.py` really on a separate line? Also, is this due to something in the python script? What happens if you replace the script with one that just prints "running" or something similar? – Gordon Davisson Feb 02 '19 at 00:52
  • I had a similar issue some time ago, check [this answer](https://stackoverflow.com/a/25638318/2314737) hth! – user2314737 Feb 02 '19 at 06:45
  • Yes, it is on a separate line. I emphasized this fact in the description, because I know it looks as if it wasn't. Python script works flawlessly on its own i.e when I run it in the terminal "python test.py", I get the result I want. – SkogensKonung Feb 02 '19 at 11:05

4 Answers4

15

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)
Barry
  • 151
  • 1
  • 5
  • Hi Barry, it is better if you don't post links as an answer, welcome to SO – Bruno Jan 03 '20 at 19:01
  • Thanks for the tip. Is that in general? For instance, I could have included the info in my answer directly but I would still want to give the guy credit by including the link. – Barry Jan 04 '20 at 03:06
  • I have given it full disk access. Still doesn't work. I am using Mojave as well. Any other reason why cron is not executing? – mythicalcoder May 07 '20 at 15:47
  • Thanks for this answer, it was just simple and effective – Kev Jul 07 '21 at 03:36
0

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
0e1val
  • 481
  • 3
  • 6
  • I've just tried what you suggest. Sadly, to no avail. The only difference is that it now prints: "/bin/bash: Applications: command not found" – SkogensKonung Feb 01 '19 at 22:28
0

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

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31
0

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...
Mark_Anderson
  • 1,229
  • 1
  • 12
  • 34