问题描述
我想创建,运行一个脚本每5秒cron作业。眼看cron作业只允许0-59分钟为增量等。
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.
我想创建一个脚本,调用下面我写的原始脚本。
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. :)
如果有运行一个脚本每5秒请指教一个简单的方法。
if there is an easier way to run a script every 5 seconds please advise.
请和谢谢。
推荐答案
我不会用cron这一点。我会使用bash脚本(使用绝对路径,除非你希望它是便携式和知道的目录结构将是preserved)。
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 know that the directory structure will be preserved).
相反,我只想睡眠5
,就像你一样(只有5秒,而不是1)。
Instead, I would just sleep 5
, just like you did (only 5 seconds instead of 1).
至于与您的系统,这取决于在系统上启动它。在(部分)的Linux发行版,有一个名为的/etc/rc.local
您可以在其中添加脚本在系统启动时运行的文件。嗯......我不应该那么一般情况下,我已经使用了发行版都没有了。如果你正在运行Ubuntu,也不再是一个inittab中,他们使用的新贵,顺便说一句。
As far as starting it with your system, that depends on the system. On (some) Linux distros, there's a file called /etc/rc.local
in 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.
所以,如果你有一个无限循环,并在的/etc/rc.local
,那么你就应该金色它无休止地运行(或直到遇到一个条目问题并退出)。
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).
这篇关于cron作业 - 运行每5秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!