V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sockball07
V2EX  ›  程序员

crontab 未执行的问题

  •  
  •   sockball07 · 2023-03-27 16:16:18 +08:00 · 1479 次点击
    这是一个创建于 395 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先建立一个 2023 年 03 月 27 日 17:00 执行的脚本并保存
    0 17 27 3 * /bin/sh /tmp/test.sh >> /tmp/test.log

    假设当前时间还未到 17 点,现将系统时间改为 17:00
    date -s '2023/03/27 17:00:00'

    之后会按预期运行一次脚本
    然后再将系统时间同步回当前时间
    /usr/sbin/ntpdate ntp.sjtu.edu.cn

    假设此时还未到 17:00
    待到时间到达 17:00 时,脚本并未按预期执行,这是什么原因呢?

    第 1 条附言  ·  2023-03-27 17:10:38 +08:00
    回复的似乎都未注意到,第一次修改系统时间后是有正确执行的,说明和命令本身无关
    另外已多次尝试,可反复复现
    第 2 条附言  ·  2023-03-28 13:39:08 +08:00
    已完结
    原因:见 8 楼
    解决方法:重新同步系统时间后(即倒回时间),重启 cron/crond 服务即可
    zeromovie
        1
    zeromovie  
       2023-03-27 16:18:43 +08:00
    我之前遇到这个问题时脚本的解释器是 sh,我改成 bash 就好了
    bruce0
        2
    bruce0  
       2023-03-27 16:25:23 +08:00
    看一下 /var/log/cron 的 log 呢
    pollux
        3
    pollux  
       2023-03-27 16:42:18 +08:00
    还有其它的可能性是环境变量和权限的问题,建议看看 log 或者 在脚本里输出一些 debug 信息到 /tmp 下(因为这个目录肯定是有权限的),看看真的有没有执行?
    sockball07
        4
    sockball07  
    OP
       2023-03-27 16:45:03 +08:00
    @zeromovie #1 你这个就是另一个问题了,我这边实际上是别的任务,上面是随便写的(没想到 sh 还有别的问题...)

    @bruce0 #2 没有这个文件😂
    saleacy
        5
    saleacy  
       2023-03-27 16:48:42 +08:00
    date 没用绝对路径
    Sekai
        6
    Sekai  
       2023-03-27 17:06:41 +08:00
    1. 语法错误:crontab 脚本中语法错误会导致脚本无法执行,需要检查语法错误并修正。

    2. 环境变量问题:crontab 脚本执行时环境变量可能与手动执行时不同,需要在脚本中指定环境变量或者在 crontab 中设置环境变量。

    3. 文件权限问题:crontab 脚本所在的目录或者脚本本身的权限可能不足以执行,需要检查并设置正确的权限。

    4. 路径问题:crontab 脚本中使用的命令或者文件路径可能与实际不符,需要检查并修正。

    5. 定时任务设置错误:crontab 定时任务设置错误会导致脚本无法执行,需要检查定时任务设置是否正确。

    6. 系统服务问题:如果系统服务未正常运行,可能会导致 crontab 脚本无法执行,需要检查并修复系统服务。

    7. 日志记录问题:crontab 脚本执行时可能会产生日志记录,需要检查日志记录是否正常并且是否有错误提示。
    levenwindy
        7
    levenwindy  
       2023-03-27 17:11:35 +08:00 via Android
    很多种可能
    1.不同系统版本都有区别
    /etc/crontab 这种是得写用户名
    * * * * * root 命令
    另一种
    ../crontabs/root 命令 (后面不需要加用户名)
    2.有些版本最后一行不会执行,因此最后要空一行
    3.命令的绝对路径
    4.sh 和 bash 的区别

    记得的就这些了,应该发到 linux 板块
    之后排除法自己去测试每一步
    jackyzy823
        8
    jackyzy823  
       2023-03-27 17:11:53 +08:00   ❤️ 1
    https://unix.stackexchange.com/questions/231209/cronjob-based-on-system-time-or-its-own-time-elapse

    > Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of daylight savings time. If the time has moved forwards, those jobs which would have run in the time that was skipped will be run soon after the change. Conversely, if the time has moved backwards by less than 3 hours, those jobs that fall into the repeated time will not be re-run.
    sockball07
        9
    sockball07  
    OP
       2023-03-27 17:26:17 +08:00
    @levenwindy #7 发贴少确实不知道往哪个板块发好...

    @jackyzy823 #8 感谢,这样原因就清楚了,想解决的话只能重新编辑 crontab 咯
    listenfree
        10
    listenfree  
       2023-03-27 17:32:09 +08:00
    Special considerations exist when the clock is changed by less than 3 hours, for example at the beginning and end of daylight savings time. If the time has
    moved forwards, those jobs which would have run in the time that was skipped will be run soon after the change. Conversely, if the time has moved backwards by
    less than 3 hours, those jobs that fall into the repeated time will not be re-run.
    iooco
        11
    iooco  
       2023-03-28 10:00:11 +08:00
    我发现通过 azure 控制台重启机器 cron 很大概率挂掉 必须 crontab -e 才能再次运行 状态依旧是 running
    heywin
        12
    heywin  
       2023-03-28 12:04:34 +08:00
    先看下系统收件箱有没有错误报告, 看下打开报告功能没.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3336 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:10 · PVG 20:10 · LAX 05:10 · JFK 08:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.