linux定时任务如何每隔0.5秒执行一次呢?
天蓬老师
天蓬老师 2017-04-17 12:06:47
0
8
914

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(8)
Ty80
* * * * * 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

黄舟

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

洪涛

Try it with watch
watch -n 0.5 date

迷茫

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

阿神

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

阿神

Let’s talk about your usage scenarios

黄舟

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

巴扎黑

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!