ddllzz
V2EX  ›  问与答

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

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

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

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