V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ddllzz
V2EX  ›  问与答

如何保证 crontab 中的定时命令,同时只有一个进程在执行

  •  
  •   ddllzz · Jun 21, 2021 · 1791 views
    This topic created in 1775 days ago, the information mentioned may be changed or developed.

    例如每分钟执行一次,如果执行时发现之前的任务还在执行中,那么自动跳过这一次。

    6 replies    2021-06-21 13:23:52 +08:00
    Tink
        1
    Tink  
    PRO
       Jun 21, 2021   ❤️ 1
    这个工作不应该在 crontab 里完成,而应该在任务或者脚本中完成
    ddllzz
        2
    ddllzz  
    OP
       Jun 21, 2021
    @Tink #1 在任务中通过什么可靠的方式来判断呢
    beyond_st
        3
    beyond_st  
       Jun 21, 2021   ❤️ 1
    lock file
    Tink
        4
    Tink  
    PRO
       Jun 21, 2021 via Android   ❤️ 2
    @ddllzz pidfile 就行
    cnxobo
        5
    cnxobo  
       Jun 21, 2021   ❤️ 2
    * * * * * (flock -xn ./test.lock -c "sh /root/test.sh")
    -n 为非阻塞模式
    -x 获取一个排它锁
    Vegetable
        6
    Vegetable  
       Jun 21, 2021   ❤️ 1
    crontab 这么实现挺麻烦的。这种我会选择 systemd 的 timer 实现。上次执行完成后+60s 执行下一次。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1089 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    ♥ Do have faith in what you're doing.