简体   繁体   English

crontab Mac每天同时运行Python脚本

[英]crontab Mac running a Python script everyday at the same time

Tried to follow the instructions from this question but it didn't work with me. 尝试按照此问题的说明进行操作,但对我不起作用。 As background info I can tell you that there's an instance of Python 2.7 installed on Mac by default and my instance is actually python 3.6, hence the Python3. 作为背景信息,我可以告诉您,默认情况下Mac上安装了Python 2.7实例,而我的实例实际上是python 3.6,因此是Python3。 I'm doing something wrong but I can't figure out what. 我做错了,但我不知道是什么。 I know that as is it will run every minute, I just want to make it work first and then change the time. 我知道它会每分钟运行一次,所以我只想先使其运行,然后更改时间。

* * * * * cd /User/Users/cannopy/PycharmProjects/untitled python3 /User/Users/cannopy/PycharmProjects/untitled/test.py

EDIT: 编辑:

After changing some things I managed to make it work with this: 更改了一些内容后,我设法使其适用于此:

* * * * * cd /Users/cannopy/PycharmProjects/untitled/ && ~/PycharmProjects/untitled/venv/bin/python test.py

尝试* * * * * cd /User/Users/cannopy/PycharmProjects/untitled && python3 test.py >> cron_result.txt

A crontab entry can only contain a single line. crontab条目只能包含一行。 You can combine commands with ; 您可以将命令与; or && but ideally your Python script should not require to run in a particular directory. &&但理想情况下,您的Python脚本不需要在特定目录中运行。

If the script requires imports or data files from the directory where it is installed, maybe have it examine sys.argv[0] to discover its location. 如果脚本要求从安装目录导入或数据文件,则可以让该脚本检查sys.argv[0]以发现其位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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