每30秒跑一次cron。

时间:2021-04-11 01:44:12

Ok so i have a cron that i need to run every 30 seconds...here is what i have below

好的,我有一个cron,我需要每30秒跑一次…以下是我所拥有的。

*/30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''

It runs but is this 30 minutes or 30 seconds...and also I have been reading that cron might not be the best tool to use if I run it that often. Is there another better tool that i can install on ubuntu 11.04 that will be a better option or is there a way to fix the above cron

它运行的时间是30分钟或30秒……而且我一直在阅读,如果我经常运行cron,它可能不是最好的工具。我可以在ubuntu 11.04上安装另一个更好的工具吗?这将是一个更好的选择,或者有办法修复上面的cron吗?

12 个解决方案

#1


477  

You have */30 in the minutes specifier - that means every minute but with a step of 30 (in other words, every half hour). Since cron does not go down to sub-minute resolutions, you will need to find another way.

每分钟你有*/30,这意味着每一分钟,但有30步(换句话说,每半小时)。因为cron不属于分分钟的决议,所以你需要找到另一种方法。

One possibility, though it's a bit of a kludge, is to have two jobs, one offset by 30 seconds:

一种可能性是,尽管它有点笨拙,但它有两份工作,一份抵消了30秒:

* * * * * /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )

Both cron jobs actually run every minute but the latter one will wait half a minute before executing the "meat" of the job, /path/to/executable.

两个cron作业实际上每分钟都在运行,但后者在执行作业的“肉”/路径/to/可执行文件之前要等半分钟。

#2


46  

You can't. Cron has a 60 sec granularity.

你不能。Cron的粒度为60秒。

* * * * * cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
* * * * * sleep 30 && cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''

#3


33  

Cron's granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop and it should do what you need:

Cron的粒度是在几分钟内的,并不是为了在每一个x秒内唤醒某物而设计的。在循环中运行您的重复任务,它应该满足您的需要:

#!/bin/env bash
while [ true ]; do
 sleep 30
 # do what you need to here
done

#4


16  

No need for two cron entries, you can put it into one with:

不需要两个cron条目,你可以把它放在一起:

* * * * * /bin/bash -l -c "/path/to/executable; sleep 30 ; /path/to/executable"

so in your case:

所以在你的情况中:

* * * * * /bin/bash -l -c "cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'' ; sleep 30 ; cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *insert_latest \”;睡眠30;cd /srv/last_song/发布/20120308133159 &脚本/rails runner -e production '\ " Song.insert_latest'\ "

#5


10  

You can check out my answer to this similar question

你可以看看我对这个相似问题的答案。

Basically, I've included there a bash script named "runEvery.sh" which you can run with cron every 1 minute and pass as arguments the real command you wish to run and the frequency in seconds in which you want to run it.

基本上,我已经包含了一个名为“runEvery”的bash脚本。你可以用cron每1分钟运行一次,然后作为参数传递给你想要运行的实际命令和你想要运行它的秒数。

something like this

像这样的东西

*/1 * * * * ~/bin/runEvery.sh 5 myScript.sh

*/1 * * * * ~/bin/runEvery。sh 5 myScript.sh

#6


8  

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.

Cron作业不能用于在秒间隔内安排作业。我。你不能安排一个cron作业每5秒钟运行一次。另一种方法是编写一个shell脚本,其中使用sleep 5命令。

Create a shell script every-5-seconds.sh using bash while loop as shown below.

每5秒钟创建一个shell脚本。使用bash while循环,如下所示。

$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.sh
 sleep 5
done

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

现在,使用nohup在后台执行这个shell脚本,如下所示。即使在您退出会话之后,它仍将继续执行脚本。这将执行您的备份。sh shell脚本每5秒。

$ nohup ./every-5-seconds.sh &

#7


4  

Use watch:

使用注意:

$ watch --interval .30 script_to_run_every_30_sec.sh

#8


4  

Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds. The alternative :

Crontab job可以用来在几分钟/小时/天内安排一份工作,但不能在几秒钟内完成。另一种选择:

Create a script to execute every 30 seconds:

创建一个脚本,每30秒执行一次:

#!/bin/bash
# 30sec.sh

for COUNT in `seq 29` ; do
  cp /application/tmp/* /home/test
  sleep 30
done

Use crontab -e and a crontab to execute this script:

使用crontab -e和crontab来执行这个脚本:

* * * * * /home/test/30sec.sh > /dev/null

#9


3  

Use fcron (http://fcron.free.fr/) - gives you granularity in seconds and way better and more feature rich than cron (vixie-cron) and stable too. I used to make stupid things like having about 60 php scripts running on one machine in very stupid settings and it still did its job!

使用fcron (http://fcron.free.fr/)——在几秒钟内提供粒度,比cron (vixie-cron)和稳定的功能更丰富。我曾经做过一些愚蠢的事情,比如在一个机器上运行了大约60个php脚本,在非常愚蠢的设置中,它仍然执行它的工作!

#10


0  

I just had a similar task to do and use the following approach :

我只是做了一个类似的任务,并使用以下方法:

nohup watch -n30 "kill -3 NODE_PID" &

I needed to have a periodic kill -3 (to get the stack trace of a program) every 30 seconds for several hours.

我需要一个周期的kill -3(以获取程序的堆栈跟踪),每30秒持续几个小时。

nohup ... & 

This is here to be sure that I don't lose the execution of watch if I loose the shell (network issue, windows crash etc...)

这是为了确保如果我松开shell(网络问题、windows崩溃等),我不会失去监视的执行。

#11


0  

in dir /etc/cron.d/

在迪尔/etc/cron.d/

new create a file excute_per_30s

新建一个文件excute_per_30s。

* * * * * yourusername  /bin/date >> /home/yourusername/temp/date.txt
* * * * * yourusername sleep 10; /bin/date >> /home/yourusername/temp/date.txt

will run cron every 30 seconds

每30秒跑一次cron ?

#12


0  

Thanks for all the good answers. To make it simple I liked the mixed solution, with the control on crontab and the time division on the script. So this is what I did to run a script every 20 seconds (three times per minute). Crontab line:

谢谢你的回答。为了使它简单,我喜欢混合的解决方案,在crontab和脚本的时间划分上进行控制。这就是我每20秒运行一个脚本的方法(每分钟3次)。Crontab行:

 * * * * 1-6 ./a/b/checkAgendaScript >> /home/a/b/cronlogs/checkAgenda.log

Script:

脚本:

cd /home/a/b/checkAgenda

java -jar checkAgenda.jar
sleep 20
java -jar checkAgenda.jar 
sleep 20
java -jar checkAgenda.jar 

#1


477  

You have */30 in the minutes specifier - that means every minute but with a step of 30 (in other words, every half hour). Since cron does not go down to sub-minute resolutions, you will need to find another way.

每分钟你有*/30,这意味着每一分钟,但有30步(换句话说,每半小时)。因为cron不属于分分钟的决议,所以你需要找到另一种方法。

One possibility, though it's a bit of a kludge, is to have two jobs, one offset by 30 seconds:

一种可能性是,尽管它有点笨拙,但它有两份工作,一份抵消了30秒:

* * * * * /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )

Both cron jobs actually run every minute but the latter one will wait half a minute before executing the "meat" of the job, /path/to/executable.

两个cron作业实际上每分钟都在运行,但后者在执行作业的“肉”/路径/to/可执行文件之前要等半分钟。

#2


46  

You can't. Cron has a 60 sec granularity.

你不能。Cron的粒度为60秒。

* * * * * cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''
* * * * * sleep 30 && cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''

#3


33  

Cron's granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop and it should do what you need:

Cron的粒度是在几分钟内的,并不是为了在每一个x秒内唤醒某物而设计的。在循环中运行您的重复任务,它应该满足您的需要:

#!/bin/env bash
while [ true ]; do
 sleep 30
 # do what you need to here
done

#4


16  

No need for two cron entries, you can put it into one with:

不需要两个cron条目,你可以把它放在一起:

* * * * * /bin/bash -l -c "/path/to/executable; sleep 30 ; /path/to/executable"

so in your case:

所以在你的情况中:

* * * * * /bin/bash -l -c "cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'' ; sleep 30 ; cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''"

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *insert_latest \”;睡眠30;cd /srv/last_song/发布/20120308133159 &脚本/rails runner -e production '\ " Song.insert_latest'\ "

#5


10  

You can check out my answer to this similar question

你可以看看我对这个相似问题的答案。

Basically, I've included there a bash script named "runEvery.sh" which you can run with cron every 1 minute and pass as arguments the real command you wish to run and the frequency in seconds in which you want to run it.

基本上,我已经包含了一个名为“runEvery”的bash脚本。你可以用cron每1分钟运行一次,然后作为参数传递给你想要运行的实际命令和你想要运行它的秒数。

something like this

像这样的东西

*/1 * * * * ~/bin/runEvery.sh 5 myScript.sh

*/1 * * * * ~/bin/runEvery。sh 5 myScript.sh

#6


8  

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses sleep 5 command in it.

Cron作业不能用于在秒间隔内安排作业。我。你不能安排一个cron作业每5秒钟运行一次。另一种方法是编写一个shell脚本,其中使用sleep 5命令。

Create a shell script every-5-seconds.sh using bash while loop as shown below.

每5秒钟创建一个shell脚本。使用bash while循环,如下所示。

$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.sh
 sleep 5
done

Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.

现在,使用nohup在后台执行这个shell脚本,如下所示。即使在您退出会话之后,它仍将继续执行脚本。这将执行您的备份。sh shell脚本每5秒。

$ nohup ./every-5-seconds.sh &

#7


4  

Use watch:

使用注意:

$ watch --interval .30 script_to_run_every_30_sec.sh

#8


4  

Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds. The alternative :

Crontab job可以用来在几分钟/小时/天内安排一份工作,但不能在几秒钟内完成。另一种选择:

Create a script to execute every 30 seconds:

创建一个脚本,每30秒执行一次:

#!/bin/bash
# 30sec.sh

for COUNT in `seq 29` ; do
  cp /application/tmp/* /home/test
  sleep 30
done

Use crontab -e and a crontab to execute this script:

使用crontab -e和crontab来执行这个脚本:

* * * * * /home/test/30sec.sh > /dev/null

#9


3  

Use fcron (http://fcron.free.fr/) - gives you granularity in seconds and way better and more feature rich than cron (vixie-cron) and stable too. I used to make stupid things like having about 60 php scripts running on one machine in very stupid settings and it still did its job!

使用fcron (http://fcron.free.fr/)——在几秒钟内提供粒度,比cron (vixie-cron)和稳定的功能更丰富。我曾经做过一些愚蠢的事情,比如在一个机器上运行了大约60个php脚本,在非常愚蠢的设置中,它仍然执行它的工作!

#10


0  

I just had a similar task to do and use the following approach :

我只是做了一个类似的任务,并使用以下方法:

nohup watch -n30 "kill -3 NODE_PID" &

I needed to have a periodic kill -3 (to get the stack trace of a program) every 30 seconds for several hours.

我需要一个周期的kill -3(以获取程序的堆栈跟踪),每30秒持续几个小时。

nohup ... & 

This is here to be sure that I don't lose the execution of watch if I loose the shell (network issue, windows crash etc...)

这是为了确保如果我松开shell(网络问题、windows崩溃等),我不会失去监视的执行。

#11


0  

in dir /etc/cron.d/

在迪尔/etc/cron.d/

new create a file excute_per_30s

新建一个文件excute_per_30s。

* * * * * yourusername  /bin/date >> /home/yourusername/temp/date.txt
* * * * * yourusername sleep 10; /bin/date >> /home/yourusername/temp/date.txt

will run cron every 30 seconds

每30秒跑一次cron ?

#12


0  

Thanks for all the good answers. To make it simple I liked the mixed solution, with the control on crontab and the time division on the script. So this is what I did to run a script every 20 seconds (three times per minute). Crontab line:

谢谢你的回答。为了使它简单,我喜欢混合的解决方案,在crontab和脚本的时间划分上进行控制。这就是我每20秒运行一个脚本的方法(每分钟3次)。Crontab行:

 * * * * 1-6 ./a/b/checkAgendaScript >> /home/a/b/cronlogs/checkAgenda.log

Script:

脚本:

cd /home/a/b/checkAgenda

java -jar checkAgenda.jar
sleep 20
java -jar checkAgenda.jar 
sleep 20
java -jar checkAgenda.jar