Linux Cron:每 1 秒运行一次 cron?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8670328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:51:46  来源:igfitidea点击:

Cron: running cron every 1 second?

linuxcron

提问by Crazy_Bash

How can I run cron every 1 second? there's only minutes option by default

如何每 1 秒运行一次 cron?默认情况下只有分钟选项

采纳答案by Pete Wilson

Let cron start the job one time, the first time. Put the program in an infinite loop, sleep() for 1 second at the end of each loop. like this, in C:

让 cron 开始工作一次,第一次。将程序置于无限循环中,在每个循环结束时 sleep() 1 秒钟。像这样,在 C 中:

  int main( int argc, char ** argv ) {
      while (1) {
        // do the work
        sleep(1000);
      }
  }

Could that work?

那能行吗?

回答by Marc B

You can't with cron, because 1 minute is THEminimum time interval available. You'd have to run a script that fires up 60 other scripts, with delays of 0 to 59 seconds, or a single script which re-runs itself 60 times.

你不能用cron的,因为1分钟时最小时间间隔可用。您必须运行一个脚本来启动 60 个其他脚本,延迟为 0 到 59 秒,或者一个脚本可以重新运行 60 次。

But at that point, why not just run a single script outside of cron which does sleep(1) in a loop?

但是在这一点上,为什么不只在 cron 之外运行一个脚本来循环执行 sleep(1) 呢?

回答by UtahJarhead

Cron executes stuff every minute. Use a script:

Cron 每分钟执行一次。使用脚本:

while :
do
    sleep 1
    some_command || break
done

or in one line:

或在一行中:

while : ; do sleep 1 ; some_command || break ; done

This will wait 1 second in between each execution, so if your command takes .75 seconds to run, then this script will kick it off every 1.75 seconds.

这将在每次执行之间等待 1 秒,因此如果您的命令需要 0.75 秒来运行,那么此脚本将每 1.75 秒启动一次。