Heim  >  Fragen und Antworten  >  Hauptteil

linux定时任务如何每隔0.5秒执行一次呢?

求一个例子!!!linux定时任务如何每隔0.5秒执行一次呢?

天蓬老师天蓬老师2743 Tage vor921

Antworte allen(8)Ich werde antworten

  • PHP中文网

    PHP中文网2017-04-17 12:08:47

    * * * * * for i in `seq 120`; do awesome_scripts& sleep 0.5; done
    

    由crontab每分钟调度,每次唤起,循环执行120次,间隔0.5秒

    Antwort
    0
  • 黄舟

    黄舟2017-04-17 12:08:47

    由于 crontab 最小单位是分钟,只能自己写程序执行了。给个 shell + python 的例子:

    while true ; do ./your-script & ; python -c "import time;time.sleep(0.5)"; done
    

    或者 python + shell:

    import time
    from subprocess import call
    while(True):
        call(["./your-script"])
        time.sleep(0.5)
    

    没有 linux 所以没有亲测,大概就是这么个思路

    Antwort
    0
  • PHP中文网

    PHP中文网2017-04-17 12:08:47

    watch
    watch -n 0.5 date试试看

    Antwort
    0
  • 迷茫

    迷茫2017-04-17 12:08:47

    windows机器不方便试,大概这样的

    #!/bin/bash
    for i in {1..120}
    do
      usleep 500
      RUN_YOUR_TASK&
    done
    

    用usleep睡半秒,持续一分钟,结合cron就行了

    Antwort
    0
  • 阿神

    阿神2017-04-17 12:08:47

    没找到啊。只能到分钟级别,秒都到不了。

    Antwort
    0
  • 阿神

    阿神2017-04-17 12:08:47

    还是说说你的使用场景吧

    Antwort
    0
  • 黄舟

    黄舟2017-04-17 12:08:47

    crontab 最短定义只能到分钟,只能用脚本sleep 来完成

    Antwort
    0
  • 巴扎黑

    巴扎黑2017-04-17 12:08:47

    以上所有方法,放在linux上执行时,会出一个坑,出现多个进程.
    因为若遇到延时,超出60秒后,第二个定时任务启动.就会出现多个进程.
    解决方法,加文件锁/usr/bin/flock

    Antwort
    0
  • StornierenAntwort