Cron 作业——每 5 秒运行一次 [英] Cron jobs -- to run every 5 seconds

查看:70
本文介绍了Cron 作业——每 5 秒运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建每 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 脚本(使用绝对路径,除非你希望它是可移植的并且知道目录结构将被保留).

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).

相反,我会像你一样 sleep 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,他们使用 upstart,顺便说一句.

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 秒运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆