我有一个cron需要每30秒运行一次。

以下是我所拥有的:

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

它能运行,但它是每30分钟或30秒运行一次吗?

此外,我一直在阅读,如果我经常运行cron,它可能不是最好的工具。有没有其他更好的工具,我可以使用或安装在Ubuntu 11.04上,这将是一个更好的选择?有没有办法修复上面的cron?


当前回答

Crontab job可用于以分钟/小时/天为单位调度作业,但不能以秒为单位。替代方案:

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

#!/bin/bash
# 30sec.sh

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

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

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

其他回答

在你的肝等等

新建文件excute_per_30s

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

是否每30秒运行一次cron

Cron的粒度以分钟为单位,并不是设计为每x秒唤醒一次以运行某项操作。在循环中运行你的重复任务,它应该做你需要的:

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

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

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

在你的例子中:

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

使用fcron (http://fcron.free.fr/) -提供以秒为单位的粒度,比cron (vixie-cron)更好,功能更丰富,也更稳定。我曾经做过一些愚蠢的事情,比如在一台机器上运行60个php脚本,设置非常愚蠢,但它仍然发挥了作用!

我刚刚有一个类似的任务要做,并使用了以下方法:

nohup watch -n30 "kill -3 NODE_PID" &

我需要在几个小时内每30秒执行一次定期kill -3(以获得程序的堆栈跟踪)。

nohup ... & 

这是在这里确保我不会失去手表的执行,如果我松散的外壳(网络问题,windows崩溃等…)