【问题标题】:Why my crontab can not work with /etc/crontab file为什么我的 crontab 不能使用 /etc/crontab 文件
【发布时间】:2019-06-11 11:15:35
【问题描述】:

在我的项目中,我想用 cron 做一个计划任务。

所以我在 /etc/crontab 中添加了一行

*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh

/etc/crontab 的内容是这样的:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*/10 * * * * root /home/JobidUserJobname/JobidUserJobname.sh
*/1 * * * * root date

然后我重新启动 crontab 服务:

#service crond restart
#service crond reload

他们成功执行了。

但是当我执行时:

#crontab -l

它显示:

no crontab for root

好像没什么问题。我的 linux 操作系统是:

CentOS release 6.5 (Final)

谁能帮帮我?

【问题讨论】:

  • 这对于Unix & Linux 来说可能是一个更好的问题。这不是一个真正的编程问题(除非您认为crontab 是一种简单的编程语言)。
  • 这也可能是这个问题的重复:unix.stackexchange.com/q/127732/10454——你需要了解系统crontab和用户(包括root)crontab的区别。

标签: cron


【解决方案1】:

crontab -l 给出输出“root 没有 crontab”。这是意料之中的,因为它只会显示使用命令 crontab -e 添加的 crontab。在“/etc/crontab”中添加的 crons 不会在 crontab -l 命令中列出。

检查事项。 1、检查文件“/home/JobidUserJobname/JobidUserJobname.sh”是否有执行权限。如果不是,则执行以下命令。

chmod +x /home/JobidUserJobname/JobidUserJobname.sh
  1. 如果仍然无法正常工作,请在脚本前添加“/bin/sh”。

    */10 * * * * root /bin/sh /home/JobidUserJobname/JobidUserJobname.sh

  2. 检查 cron 日志以查看是否有任何错误。

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2016-09-24
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多