如何每 20 秒执行一次 bash 脚本? ,好用sleep和loop?

how to execute a bash script every 20 seconds? , good use sleep and a loop?

我现在在 cron 中每分钟都有一个脚本 运行ning,我想每 20 秒运行

另一方面,服务器管理器(脚本在由其他人管理的远程机器中)抱怨 crons 运行ning 经常超过 5 ~ 10 分钟

我该如何编程?

我应该为每个循环使用 while true 循环和 sleep 20 吗?

我认为你循环和睡眠的想法是正确的方法。

每次系统启动时(从 cron),您都可以在后台启动脚本,其中的条目使用 @reboot:

@reboot /path/to/script argument1 argument2

确保将输出写入文件,而不是 STDOUT。

您可以使用 nohup:

在不重新启动系统的情况下测试您的脚本
$ nohup /path/to/script argument1 argument2 &

运行 通过 cron 像这样:

* * * * *             /path/to/script
* * * * * sleep 20 && /path/to/script
* * * * * sleep 40 && /path/to/script