[英]Running script with cron doesn't work on mac
I keep trying to run a cron job that executes a python script every minute.我一直在尝试运行一个每分钟执行一个 python 脚本的 cron 作业。 Having executed "which python", I set up the cron job as follows:
执行完“which python”后,我将 cron 作业设置如下:
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. Job 的描述在一行中,并且在定义的末尾有一个换行符。
I get the following error in /var/mail/MyMac
:我在
/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.然后我尝试了 /usr/bin/python 的所有可能组合,无论是否有 lib、anaconda 等,无论是否指定 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.所以我做了一个 cron Job 作为 sudo。 The path in the env.output is:
env.output 中的路径是:
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:最后,我将我的 cronjob(作为普通用户而不是 sudo)设置为:
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
#!/Users/MyMac/anaconda3/bin/python
chmod +x /Users/MyMac/desktop/cron_test/test.py
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 minute* * * * * * /Users/MyMac/desktop/cron_test/test.py
这是一种告诉 cron 每分钟执行一次作业的形式Try to make cron record like this:尝试像这样制作 cron 记录:
* * * * * /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在
MAILTO
cron 上需要电子邮件地址,而不是某个名称
All existing answers to this question (while helpful general cron suggestions) are wrong.这个问题的所有现有答案(虽然有用的一般 cron 建议)都是错误的。
OP error is a cron with six time entries, not five: OP 错误是一个有六个时间条目的 cron,而不是五个:
*/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.因此,cron 将前五个条目作为时间指令读取,将第六个
*
作为终端指令(扩展为当前目录中文件/目录列表的通配符),然后尝试将该列表中的第一个字符串作为命令并给出一个错误。
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...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.