Home  >  Q&A  >  body text

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

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

天蓬老师天蓬老师2743 days ago919

reply all(8)I'll reply

  • PHP中文网

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

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

    Scheduled by crontab every minute, each time it is invoked, the loop is executed 120 times with an interval of 0.5 seconds

    reply
    0
  • 黄舟

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

    Since crontab the minimum unit is minutes, you can only write your own program to execute it. Give an example of shell + python:

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

    or python + shell:

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

    I don’t have linux, so I haven’t tested it myself. That’s probably the idea

    reply
    0
  • PHP中文网

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

    Try it with watch
    watch -n 0.5 date

    reply
    0
  • 迷茫

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

    It’s not convenient to test on a Windows machine, maybe something like this

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

    Use usleep to sleep for half a second for one minute, just combine it with cron

    reply
    0
  • 阿神

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

    Not found. It can only reach the level of minutes, not even seconds.

    reply
    0
  • 阿神

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

    Let’s talk about your usage scenarios

    reply
    0
  • 黄舟

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

    The shortest definition of crontab can only be up to minutes, which can only be completed with the script sleep

    reply
    0
  • 巴扎黑

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

    When all the above methods are executed on Linux, a pit will appear and multiple processes will appear.
    Because if there is a delay, after more than 60 seconds, the second scheduled task will start. Multiple processes will appear. process.
    The solution is to add file lock /usr/bin/flock

    reply
    0
  • Cancelreply