如何在bash脚本检查,如果事情是运行和退出,如果它是 [英] How to check in a bash script if something is running and exit if it is

查看:86
本文介绍了如何在bash脚本检查,如果事情是运行和退出,如果它是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行每隔15分钟,但有时如果盒子是忙碌它挂起和下道工序将开始第一个完成创建一个滚雪球效应之前的脚本。我如何添加几行到bash脚本来检查,如果事情开始之前先运行?

I have a script that runs every 15 minutes but sometimes if the box is busy it hangs and the next process will start before the first one is finished creating a snowball effect. How can I add a couple lines to the bash script to check to see if something is running first before starting?

推荐答案

在代替PID文件的,只要你的脚本有一个唯一识别的名称,你可以做这样的事情:

In lieu of pidfiles, as long as your script has a uniquely identifiable name you can do something like this:

#!/bin/bash
COMMAND=$0
# exit if I am already running
RUNNING=`ps --no-headers -C${COMMAND} | wc -l`
if [ ${RUNNING} -gt 1 ]; then
  echo "Previous ${COMMAND} is still running."
  exit 1
fi
... rest of script ...

这篇关于如何在bash脚本检查,如果事情是运行和退出,如果它是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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