bash Cron 作业——每 5 秒运行一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3331577/
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
Cron jobs -- to run every 5 seconds
提问by myusuf3
I want to create cron job that runs a script every 5 seconds. Seeing that cron jobs only allows increments of minutes 0-59 and so on.
我想创建每 5 秒运行一次脚本的 cron 作业。看到 cron 作业只允许 0-59 分钟的增量等等。
I thought to create another script that calls my original script written below.
我想创建另一个脚本来调用我在下面写的原始脚本。
#!/bin/bash
while true
do
# script in the same directory as this script. is this correct?
bash makemehappy.sh
sleep 1
done
I now, need to know how to run this script every time i boot my computer and for it to start itself if it isn't running for some reason.
我现在需要知道如何在每次启动我的计算机时运行这个脚本,如果它由于某种原因没有运行,它会自行启动。
I am also aware that running this script every minute wouldn't be a good thing. :)
我也知道每分钟运行这个脚本不是一件好事。:)
if there is an easier way to run a script every 5 seconds please advise.
如果有更简单的方法每 5 秒运行一次脚本,请提供建议。
Please and thank you.
谢谢,麻烦您了。
回答by TCCV
I wouldn't use cron for this. I would use that bash script (use an absolute path, unless you want it to be portable and knowthat the directory structure will be preserved).
我不会为此使用 cron。我会使用那个 bash 脚本(使用绝对路径,除非你希望它是可移植的并且知道目录结构将被保留)。
Instead, I would just sleep 5, just like you did (only 5 seconds instead of 1).
相反,我会sleep 5像你一样(只有 5 秒而不是 1 秒)。
As far as starting it with your system, that depends on the system. On (some) Linux distros, there's a file called /etc/rc.localin which you can add scripts to run when the system starts. Well... I shouldn't be so general, the distros that I have used have this. If you're running Ubuntu, there is no longer an inittab, they use upstart, btw.
至于从您的系统启动它,这取决于系统。在(某些)Linux 发行版上,有一个名为的文件/etc/rc.local,您可以在其中添加要在系统启动时运行的脚本。嗯...我不应该这么笼统,我用过的发行版都有这个。如果您运行的是 Ubuntu,则不再有 inittab,他们使用 upstart,顺便说一句。
So if you have an endless loop and an entry in /etc/rc.local, then you should be golden for it to run endlessly (or until it encounters a problem and exits).
因此,如果您有一个无限循环和一个入口/etc/rc.local,那么您应该为它无休止地运行(或直到遇到问题并退出)而努力。
回答by Borealid
Try using anacron or, better yet, an init script to start when the computer starts.
尝试使用 anacron 或者更好的是使用 init 脚本在计算机启动时启动。
If you want the script to "restart itself", you'll need to run something every few minutes to check the original is still running. This can be done in inittab (/etc/inittab) or, on Ubuntu, /etc/event.d. Try man 5 inittab, looking at the 'respawn' option.
如果您希望脚本“自行重启”,则需要每隔几分钟运行一次以检查原始脚本是否仍在运行。这可以在 inittab ( /etc/inittab) 中完成,或者在 Ubuntu 上,/etc/event.d. 试试看man 5 inittab,看看'respawn' 选项。
回答by Markus Stefanko
To answer the question in the title, this is how to run a cronjob every 5 seconds :
要回答标题中的问题,这是每 5 秒运行一次 cronjob 的方法:
* * * * * /path/to/script.sh
* * * * * sleep 5 && /path/to/script.sh
* * * * * sleep 10 && /path/to/script.sh
* * * * * sleep 15 && /path/to/script.sh
* * * * * sleep 20 && /path/to/script.sh
* * * * * sleep 25 && /path/to/script.sh
* * * * * sleep 30 && /path/to/script.sh
* * * * * sleep 35 && /path/to/script.sh
* * * * * sleep 40 && /path/to/script.sh
* * * * * sleep 45 && /path/to/script.sh
* * * * * sleep 50 && /path/to/script.sh
* * * * * sleep 55 && /path/to/script.sh
It's not beautiful, but this solution comes with no extra tools nor dependencies. So if you have working cron jobs, this should work right away.
它并不漂亮,但此解决方案没有额外的工具或依赖项。因此,如果您有工作 cron 工作,这应该立即起作用。
回答by wnoise
Some crons have an @reboottime specifier (this covers all the time and date fields). If yours does, you can use that. If this is a "system" service (rather than something running for yourself), the other solutions here are probably better.
有些crons 有一个@reboot时间说明符(这涵盖了所有的时间和日期字段)。如果你的,你可以使用它。如果这是一个“系统”服务(而不是为自己运行的东西),那么这里的其他解决方案可能会更好。
回答by Chris Johnson
Init scripts are fine at boot, but don't detect if a process fails and has to be restarted. supervisorddoes a great job of detecting failed processes and restarting them. I'd recommend a script with a 5-second loop like @Tim described, but wrap supervisordaround it to make sure it keeps running.
初始化脚本在启动时很好,但不检测进程是否失败并必须重新启动。supervisord在检测失败的进程并重新启动它们方面做得很好。我会推荐一个像@Tim 描述的那样有 5 秒循环的脚本,但要环绕supervisord它以确保它继续运行。
回答by Guss
As explained in detail in my answer to a similar question, you can use SystemD timer units with whatever schedule that you want - down to a theoretical 1 nanosecond schedule with no sleepkludges
正如我在对类似问题的回答中详细解释的那样,您可以将 SystemD 计时器单元与您想要的任何计划一起使用 - 低至理论上的 1 纳秒计划,没有sleep杂乱无章
Quick overview:
快速概览:
- Setup a SystemD service to run what you want - this can be as simple as:
- 设置一个 SystemD 服务来运行你想要的——这可以很简单:
/home/myusuf3/.config/systemd/user/makemehappy.service
/home/myusuf3/.config/systemd/user/makemehappy.service
[Unit]
Description=Make me happy
[Service]
ExecStart=/home/myusuf3/.local/bin/makemehappy.sh
- Setup a SystemD timer with the schedule that you want, as documented in
man systemd.timer:
- 使用您想要的时间表设置 SystemD 计时器,如中所述
man systemd.timer:
/home/myusuf3/.config/systemd/user/makemehappy.timer
/home/myusuf3/.config/systemd/user/makemehappy.timer
[Unit]
Description=Schedule to make me happy
[Timer]
OnBootSec=5
OnUnitActiveSec=5
AccuracySec=1
- Enable and start the timer:
- 启用并启动计时器:
:
:
systemctl --user daemon-reload
systemctl --user enable makemehappy.timer
systemctl --user start makemehappy.timer
(after you enable it, it will autostart every time you start the computer, but you probably want to start it now anyway).
(启用它后,它会在您每次启动计算机时自动启动,但无论如何您可能想现在启动它)。

