使用python运行crontab [英] Running crontab with python

查看:88
本文介绍了使用python运行crontab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

  1. 验证cron是否正在运行:ps aux | grep [c]ron应该显示正在运行的cron进程
  2. 从命令中删除重定向,以便cron通过电子邮件将您的输出发送给您
  3. 在您的crontab中添加MAILTO=<email address>,以便获取电子邮件
  4. 在命令中放置python(/opt/python2.7/bin/python)的完整路径,而不仅仅是python
  5. 向crontab添加另一个命令,例如echo FOOBAR,并验证您是否收到电子邮件.
  6. ls -l /homeec2-user/code1.py?应该是/home/ec2-user/code1.py
  7. 仅使用crontab -e从未从其他平台或直接编辑文件来编辑用户的crontab.
  8. 运行crontab -l | cat -A,以便我们可以验证所有控制字符是否正确.
  1. Verify cron is running: ps aux | grep [c]ron should show a running cron process
  2. Remove the redirects from the command so that cron emails you the output
  3. Add a MAILTO=<email address> to your crontab, so that you get the email
  4. Put the full path to python (/opt/python2.7/bin/python) instead of just python in the command
  5. Add another command to crontab such as echo FOOBAR and verify that you get the email.
  6. ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
  7. Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
  8. Run crontab -l | cat -A so that we can verify all the control characters are correct.

这篇关于使用python运行crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆