使用python运行crontab [英] Running crontab with python
问题描述
Python crontab脚本似乎不起作用.当我手动运行它时,
Python crontab script doesnt seem to work. When i run it manually,
python /home/ec2-user/code1.py
它工作正常,但是当放入crontab的cron.txt文件时,不会.
it works fine but when put into cron.txt file for the crontab, doesnt.
我的crontab文件是:
My crontab file is:
@hourly python /home/ec2-user/code1.py >/dev/null 2>&1
我也尝试过
0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1
但是运气都不好.
sudo crontab -l
@hourly python /home/ec2-user/code1.py >/dev/null 2>&1
显示所有功能. 我试过 Crontab无法运行我的python脚本,并且对其他人也不太幸运
Shows everything functional. I tried Crontab not running my python script and couple others with not much luck either.
使用
PATH=/opt/python2.7/bin
MAILTO=my@email
*/5 * * * * /home/ec2-user/code1.py
我收到的电子邮件是:
/bin/sh: /home/ec2-user/code1.py : No such file or directory
但是我可以打开和编辑文件没问题.我尝试了许多不同的操作,但归结为:cron看不到该文件.
Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.
感觉就像我经历了整个> https://askubuntu.com/questions/23009 /reasons-why-crontab-does-not work 仍然没有运气
Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work and still no luck
推荐答案
- 验证cron是否正在运行:
ps aux | grep [c]ron
应该显示正在运行的cron进程 - 从命令中删除重定向,以便cron通过电子邮件将您的输出发送给您
- 在您的crontab中添加
MAILTO=<email address>
,以便获取电子邮件 - 在命令中放置python(
/opt/python2.7/bin/python
)的完整路径,而不仅仅是python
- 向crontab添加另一个命令,例如
echo FOOBAR
,并验证您是否收到电子邮件. -
ls -l /homeec2-user/code1.py
?应该是/home/ec2-user/code1.py
- 仅使用
crontab -e
从未从其他平台或直接编辑文件来编辑用户的crontab. - 运行
crontab -l | cat -A
,以便我们可以验证所有控制字符是否正确.
- Verify cron is running:
ps aux | grep [c]ron
should show a running cron process - Remove the redirects from the command so that cron emails you the output
- Add a
MAILTO=<email address>
to your crontab, so that you get the email - Put the full path to python (
/opt/python2.7/bin/python
) instead of justpython
in the command - Add another command to crontab such as
echo FOOBAR
and verify that you get the email. ls -l /homeec2-user/code1.py
? Should that be/home/ec2-user/code1.py
- Only ever edit a user's crontab with
crontab -e
never from another platform, or by editing the file directly. - Run
crontab -l | cat -A
so that we can verify all the control characters are correct.
这篇关于使用python运行crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!