我有一个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?


当前回答

编写一个shell脚本 创建.sh文件

纳米every30second.sh

并编写脚本

#!/bin/bash
For  (( i=1; i <= 2; i++ ))
do
    write Command here
    sleep 30
done

然后为该脚本设置cron crontab - e

(*****/家庭/用户名/每30秒.sh)

这个cron调用.sh文件每1分钟&在.sh文件命令在1分钟内运行2次

如果你想运行脚本5秒,那么将30替换为5,并像这样更改for循环:I <= 12;我+ +))

当你选择任意一秒时,计算60/你的秒并写入for循环

其他回答

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

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

这是简单而优雅的:

* * * * * /scripts/script.sh
* * * * *  sleep 30; /scripts/script.sh

我在这里偶然发现的。

使用注意:

$ watch --interval .30 script_to_run_every_30_sec.sh

在shell循环中运行,示例:

#!/bin/sh    
counter=1
while true ; do
 echo $counter
 counter=$((counter+1))
 if [[ "$counter" -eq 60 ]]; then
  counter=0
 fi
 wget -q http://localhost/tool/heartbeat/ -O - > /dev/null 2>&1 &
 sleep 1
done

编写一个shell脚本 创建.sh文件

纳米every30second.sh

并编写脚本

#!/bin/bash
For  (( i=1; i <= 2; i++ ))
do
    write Command here
    sleep 30
done

然后为该脚本设置cron crontab - e

(*****/家庭/用户名/每30秒.sh)

这个cron调用.sh文件每1分钟&在.sh文件命令在1分钟内运行2次

如果你想运行脚本5秒,那么将30替换为5,并像这样更改for循环:I <= 12;我+ +))

当你选择任意一秒时,计算60/你的秒并写入for循环