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

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

天蓬老师
天蓬老师

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

Antworte allen(8)
Ty80
* * * * * for i in `seq 120`; do awesome_scripts& sleep 0.5; done

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

黄舟

由于 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 所以没有亲测,大概就是这么个思路

洪涛

watch
watch -n 0.5 date试试看

迷茫

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

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

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

阿神

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

阿神

还是说说你的使用场景吧

黄舟

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

巴扎黑

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!