Linux命令每隔一段时间运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6583784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:56:57  来源:igfitidea点击:

Linux command to run script at intervals

linuxbashshellcommand

提问by kieran

I have this command that I run from a terminal in ubuntu

我有一个从 ubuntu 终端运行的命令

python2.5 /home/me/web/gae/google_appengine/dev_appserver.py /home/me/web/gae/APPLICATION/trunk

I need to stop this running and then restart it every 10 seconds - I can run this from a .sh file if necessary.

我需要停止此运行,然后每 10 秒重新启动一次 - 如有必要,我可以从 .sh 文件运行它。

What would be the best way to do this? I'd like it to all be in one script if possible so notthat keen on using cron jobs to run it - surely there is some way of doing a loop with a delay in purely in a shell script?

什么是最好的方法来做到这一点?如果可能的话,我希望它都在一个脚本中,所以那么热衷于使用 cron 作业来运行它 - 当然有某种方法可以纯粹在 shell 脚本中进行延迟循环?

The closest equivalent I can think of is JavaScript's setInterval(function(),10000);

我能想到的最接近的等价物是 JavaScript 的 setInterval(function(),10000);

采纳答案by bmk

You could try something like this:

你可以尝试这样的事情:

while true; do
  python2.5 /home/me/web/gae/google_appengine/dev_appserver.py /home/me/web/gae/APPLICATION/trunk &
  sleep 10
  kill $!
done

I.e.: Loop forever (while true), start the python script in background, wait for 10 seconds (sleep 10) and kill the background process (kill $!).

即:永远循环(while true),在后台启动python脚本,等待10秒(sleep 10)并杀死后台进程(kill $!)。

回答by c00kiemon5ter

there is sleepand atif you don't like cron

sleepat如果你不喜欢cron

echo "print after 3min again"
sleep 180  # or sleep +3m
echo "hello again, 3min passed"

Read the man pages, play with those a bit, and I think it'd be easy to build what you want, around those.

阅读手册页,稍微尝试一下,我认为围绕这​​些内容构建您想要的内容会很容易。

回答by PlagTag

I like ~$ watch -n sec command

我喜欢 ~$ watch -n sec 命令

i.E.

IE

watch -n 10 ls /home/user/specialdata

watch -n 30 csync /dir/A /remote/dir/B