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?
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:
Try doing the following:
#!/Users/MyMac/anaconda3/bin/python
chmod +x /Users/MyMac/desktop/cron_test/test.py
* * * * * * /Users/MyMac/desktop/cron_test/test.py
which is a form of telling cron to execute the job every minuteTry 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.