简体   繁体   English

Amazon EC2服务器上的Crontab

[英]Crontab on Amazon EC2 Server

I have been trying for a while now to setup a cron job on EC2 at no luck at all. 我一直在尝试在EC2上设置cron作业,这一点都不走运。 Although the cron job is listed and runs, it just doesn't find the file. 尽管已列出并运行了cron作业,但找不到文件。

Here is the cron job 这是cron工作

*/5 * * * * python /home/ec2-user/monitoring/job.py  >> /var/log/script_output.log 2>&1

It seems what we've got here is one of those rare cases when cron job doesn't run. 看来我们这里得到的是一个 那些 罕见当cron作业不运行的情况。

Here's how I would tackle this problem. 这是我解决这个问题的方法。

  • I would run python /home/ec2-user/monitoring/job.py >> /var/log/script_output.log 2>&1 in bash. 我会在bash中运行python /home/ec2-user/monitoring/job.py >> /var/log/script_output.log 2>&1 Just to check that the command can run in normal circumstances . 只是为了检查命令是否可以在正常情况下运行。

  • I would add MAILTO=your@email. 我会添加MAILTO = your @ email。 Just to get emails when something goes wrong. 只是在出现问题时获取电子邮件

  • I would run which python command and use full path to python in the cron file. 我会跑which python命令,并使用完整路径 python在cron的文件。 Let's say which python returned /usr/bin/python. 假设which python返回了/ usr / bin / python。 Then I would edit cron file like this: */5 * * * * /usr/bin/python /home/ec2-user/monitoring/job.py >> /var/log/script_output.log 2>&1 然后我将像这样编辑cron文件: */5 * * * * /usr/bin/python /home/ec2-user/monitoring/job.py >> /var/log/script_output.log 2>&1

  • I would append an empty line to the cron file. 我会将空行添加到cron文件。 Actually I would do this first. 实际上,我会先这样做。 But I'm stuck on FreeBSD's default cron. 但是我被FreeBSD的默认cron卡住了。 You're probably running Ubuntu and using Vixie Cron. 您可能正在运行Ubuntu并使用Vixie Cron。 Couldn't hurt anyway. 反正不会受伤。

  • I would run pgrep cron in bash. 我会在bash中运行pgrep cron Just to check that cron daemon is well and running . 只是为了检查cron 守护程序是否正常运行

In the end your cron should look like this: 最后,您的cron应该如下所示:

MAILTO=my@email.com

*/5 * * * * /usr/bin/python /home/ec2-user/monitoring/job.py >> /var/log/script_output.log 2>&1

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

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