在Linux中,将任务计划为时,分,秒精度 [英] In Linux, schedule task to Hour, minute, second precision
问题描述
我只想在这个确切的时间"16:22:36"运行一次外壳脚本.诸如"at"之类的实用程序没有用,因为它们没有"seconds". 睡眠"不起作用,因为循环由于某种原因提前8个小时结束:s,我在Google上进行了搜索,找不到任何工具.所以像Linux这样的庞大操作系统没有适当的任务计划程序?
I just want to run a shell scrip at say this exact time "16:22:36". utilities like "at" are useless since they don't have "seconds". "sleep" does not work since the loop is ending 8 hours ahead of time for some reason :s , I have searched all over google and couldn't find any tools. so a huge Os like Linux doesn't have a proper task scheduler?
推荐答案
标准cron
缺少第二个精度调度,因为
The standard cron
lacks second precision scheduling because
- Unix服务器管理传统上很少需要一秒钟的精度
- 它可能以一种令人不安的方式用于多用户系统,例如每秒运行此任务"
但是,借助GNU Coreutils套件中的sleep(1)
,您可以实现真正的第二精度作业调度.
However with the help of sleep(1)
from GNU Coreutils suite, you can achieve true second precision job scheduling.
示例:等待12:14:05和12:14:10
Example: wait for 12:14:05 and 12:14:10
$ crontab -l
(snip...)
14 12 * * * sleep 5; date > /tmp/plain.txt
14 12 * * * while [ "1410" -gt "$(date +\%M\%S)" ]; do /bin/sleep 0.1; done; date > /tmp/while.txt
(等待一会儿...)
$ ls -l --time-style=full-iso /tmp/*.txt
-rw-r--r-- 1 nodakai nodakai 43 2014-02-22 12:14:06.236151567 +0800 /tmp/plain.txt
-rw-r--r-- 1 nodakai nodakai 43 2014-02-22 12:14:10.007600677 +0800 /tmp/while.txt
从第一个版本中可以看到,crontab
中的14 12
不能保证12:14:00
清晰.第二版使用while
循环和亚秒级sleep(1)
来实现亚秒级精度.
As you see from the 1st version, 14 12
in crontab
does not guarantee 12:14:00
sharp. The 2nd version uses while
loop and sub-second sleep(1)
to achieve sub-second precision.
请注意,除非您使用NTP将计算机时钟与时间服务器同步,否则谈论次精确作业调度是没有意义的.
这篇关于在Linux中,将任务计划为时,分,秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!