bash脚本只运行一次

编程入门 行业动态 更新时间:2024-10-07 22:22:16
本文介绍了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脚本只运行一次

本文发布于:2023-11-24 00:49:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!