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


当前回答

目前我正在使用下面的方法。工作没有问题。

* * * * * /bin/bash -c ' for i in {1..X}; do YOUR_COMMANDS ; sleep Y ; done '

如果你想每N秒运行一次,那么X = 60/N Y = N。

其他回答

不需要两个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'\ " "

Have a look at frequent-cron - it's old but very stable and you can step down to micro-seconds. At this point in time, the only thing that I would say against it is that I'm still trying to work out how to install it outside of init.d but as a native systemd service, but certainly up to Ubuntu 18 it's running just fine still using init.d (distance may vary on latter versions). It has the added advantage (?) of ensuring that it won't spawn another instance of the PHP script unless a prior one has completed, which reduces potential memory leakage issues.

编写一个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循环

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

这是简单而优雅的:

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

我在这里偶然发现的。