在Mac中运行crontab [英] Running crontab in mac
问题描述
我正在尝试测试必须由cron作业运行的python脚本.我正在尝试在Mac中配置cron作业,但无法运行.这是我的计划工作...
I'm trying to test a python script that has to be run by a cron job. I'm trying to config the cron job in my mac but doesn't run. Here is my cron job...
* * * * * user /usr/local/bin/python3 ~/Documents/wpc/stocks/daily_stock.py
如果我 crontab -l </code>,工作就在那里.我手动运行了脚本并可以正常运行,但不能由cron作业运行.我该如何解决?
If I crontab -l
the job is there. I ran the script manually and works but is not running by the cron job. How can I fix this?
推荐答案
我使用以下方法在Mac OS上解决了此问题.
I used the following method to solve this issue on Mac OS.
您可以在Python脚本的顶部添加一个shebang以指定Python版本路径:
You can add a shebang to the top of your python script to specify the Python versions path:
Python 3
#!/usr/bin/env python3
Python 2.7
#!/usr/bin/env python2
您还需要确保文件是可执行文件:
chmod a+x filename.py
这将允许您执行python脚本,而不必在脚本之前指定"python".
This will allow you to execute the python script without having to specify "python" before the script.
然后可以使用 crontab -e
设置crontab,例如:
You can then set your crontab with crontab -e
, e.g:
0 9/15 * * * cd /Users/user12/Dev/Scripts/Python && ./test.py
这篇关于在Mac中运行crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!