CronJob没有运行 [英] CronJob not running
问题描述
我在ubuntu环境中为root用户设置了cronjob,方法是键入crontab -e
I have setup cronjob for root user in ubuntu environment as follows by typing crontab -e
34 11 * * * sh /srv/www/live/CronJobs/daily.sh
0 08 * * 2 sh /srv/www/live/CronJobs/weekly.sh
0 08 1 * * sh /srv/www/live/CronJobs/monthly.sh
但是cronjon无法运行.我尝试使用
But the cronjon do not run. I have tried checking if the cronjob is running using
pgrep cron
pgrep cron
,进程ID为3033.shell脚本调用了python文件,并用于发送电子邮件.运行python文件是可以的.没有任何错误,但cron不运行. daily.sh文件中包含以下代码.
and that gives process id 3033.The shell scrip is calls python file and is used to send email. Running the python file is ok. There's no error in it but the cron doesn't run. The daily.sh file has following code in it.
python /srv/www/live/CronJobs/daily.py
python /srv/www/live/CronJobs/notification_email.py
python /srv/www/live/CronJobs/log_kpi.py
推荐答案
WTF ?!我的cronjob无法运行?!
以下是调试未运行cronjobs的清单指南:
Here's a checklist guide to debug not running cronjobs:
- Cron守护程序是否正在运行?
- 运行
ps ax | grep cron
并查找cron. - Debian:
service cron start
或service cron restart
- 运行
- Is the Cron daemon running?
- Run
ps ax | grep cron
and look for cron. - Debian:
service cron start
orservice cron restart
- Run
-
* * * * * /bin/echo "cron works" >> /tmp/file
- 语法正确吗?参见下文.
- 您显然需要对将输出重定向到的文件具有写权限.
/tmp
中当前不存在的唯一文件名应始终可写.
* * * * * /bin/echo "cron works" >> /tmp/file
- Syntax correct? See below.
- You obviously need to have write access to the file you are redirecting the output to. A unique file name in
/tmp
which does not currently exist should always be writable.
- 通过在CLI上进行空运行来检查脚本是否有错误
- 在测试命令时,以您正在编辑crontab的用户身份进行测试,该用户可能不是您的登录名或root用户
- 检查
/var/log/cron.log
或/var/log/messages
是否存在错误. - Ubuntu:
grep CRON /var/log/syslog
- Redhat:
/var/log/cron
- Check
/var/log/cron.log
or/var/log/messages
for errors. - Ubuntu:
grep CRON /var/log/syslog
- Redhat:
/var/log/cron
- 在命令上设置可执行标志:
chmod +x /var/www/app/cron/do-stuff.php
- 如果您将命令的输出重定向到文件,请确认您具有写该文件/目录的权限
- 检查she-bangs/hashbangs线
- 不要依赖PATH之类的环境变量,因为在cron下它们的值可能与在交互式会话下不同.
- 通常使用这种抑制方式:
30 1 * * * command > /dev/null 2>&1
- 通过完全删除
>/dev/null 2>&1
重新启用标准输出或标准错误消息输出;或重定向到您具有写访问权的位置的文件:>>cron.out 2>&1
会将标准输出和标准错误附加到调用用户主目录中的cron.out
.
- commonly used is this suppression:
30 1 * * * command > /dev/null 2>&1
- re-enable the standard output or standard error message output by removing
>/dev/null 2>&1
altogether; or perhaps redirect to a file in a location where you have write access:>>cron.out 2>&1
will append standard output and standard error tocron.out
in the invoking user's home directory.
仍然无法正常工作吗? kes!
- 提高cron调试级别
- Debian
- 在
/etc/default/cron
- 设置
EXTRA_OPTS="-L 2"
-
service cron restart
-
tail -f /var/log/syslog
查看已执行的脚本
- 在
- Debian
- Raise the cron debug level
- Debian
- in
/etc/default/cron
- set
EXTRA_OPTS="-L 2"
service cron restart
tail -f /var/log/syslog
to see the scripts executed
- in
- Debian
- 在
/etc/rsyslog.d/50-default.conf
- 添加或注释行
cron.crit /var/log/cron.log
- 重新加载记录器
sudo /etc/init.d/rsyslog reload
- 重新运行cron
- 打开
/var/log/cron.log
并查找详细的错误输出
- in
/etc/rsyslog.d/50-default.conf
- add or comment out line
cron.crit /var/log/cron.log
- reload logger
sudo /etc/init.d/rsyslog reload
- re-run cron
- open
/var/log/cron.log
and look for detailed error output
Cronjob语法
# Minute Hour Day of Month Month Day of Week User Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 * * * root /usr/bin/find
对于root
用户,该语法仅正确正确.普通用户crontab
语法没有 User 字段(不允许普通用户像其他任何用户一样运行代码);
This syntax is only correct for the root
user. Regular user crontab
syntax doesn't have the User field (regular users aren't allowed to run code as any other user);
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 * * * /usr/bin/find
Crontab命令
-
crontab -l
- 列出所有用户的cron任务.
- 开始crontab文件的编辑会话.
- 退出编辑器后,会自动安装修改后的crontab.
- 从cron后台处理程序而不是crontab文件中删除crontab条目.
这篇关于CronJob没有运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!