简体   繁体   English

使用 cron 运行脚本在 mac 上不起作用

[英]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?我究竟做错了什么?

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.所以我做了一个 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:尝试这样做:

  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)键入“command+shift+G”以打开文件对话框(似乎无法导航 /usr 目录)
  6. Enter the full path name to the application (/usr/sbin/cron) and click on “Go”输入应用程序的完整路径名(/usr/sbin/cron),然后单击“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.我在 cron 中使用一个简单的 csh 脚本进行了尝试,并且成功了。 (Credit to my source: https://blog.bejarano.io/fixing-cron-jobs-in-mojave ) (归功于我的来源: 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在你的 python 脚本第一行输入: #!/Users/MyMac/anaconda3/bin/python
  • Make sure your script is executable: chmod +x /Users/MyMac/desktop/cron_test/test.py确保您的脚本是可执行的: 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将 cron 配方更改为: * * * * * * /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 nameMAILTO 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.

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