bash脚本只运行一次 [英] Bash Script run only once

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

问题描述

我想打一个sh脚本,将只在任何时候运行最多一次。说,如果我执行的是剧本,然后我再去执行的是剧本,我怎么让这个如果脚本的第一EXEC仍然是工作,第二个将因错误而失败。即我需要检查,如果脚本在那里做任何事情之前其他运行。我怎么会去这样做?

I want to make a sh script that will only run at most once at any point. Say if I exec the script then I go to exec the script again, how do I make it so that if the first exec of the script is still working the second one will fail with an error. I.e. I need to check if the script is running else where before doing anything. How would I go about doing this??

脚本我有一个运行长时间运行的过程中(即运行永远)我想使用类似cron来调用脚本的情况下,该进程失败,每15分钟左右,就会被下一个cron运行脚本重新启动..

The script I have runs a long running process (i.e. runs for ever) I wanted to use something like cron to call the script every 15mins so in case the process fails, it will be restarted by the next cron run script...

干杯。

推荐答案

您需要一个PID文件,也许是这样的:

You want a pid file, maybe something like this:

pidfile=/path/to/pidfile
if [ -f "$pidfile" ] && kill -0 `cat $pidfile` 2>/dev/null; then
    echo still running
    exit 1
fi  
echo $$ > $pidfile

这篇关于bash脚本只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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