如果正在运行,如何检入 bash 脚本,如果正在运行则退出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/927091/
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-09-09 18:13:43  来源:igfitidea点击:

How to check in a bash script if something is running and exit if it is

bashscripting

提问by Ryan Detzel

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?

我有一个脚本每 15 分钟运行一次,但有时如果盒子很忙,它会挂起,下一个进程将在第一个进程完成创建滚雪球效果之前开始。如何在 bash 脚本中添加几行以检查是否在开始之前先运行了某些内容?

采纳答案by allaryin

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

代替 pidfiles,只要您的脚本具有唯一可识别的名称,您就可以执行以下操作:

#!/bin/bash
COMMAND=
if pidof -x vim > /dev/null
then
    echo "Vim already running"
    exit 1
fi
# 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 ...

回答by rodion

You can use pidof -xif you know the process name, or kill -0if you know the PID.

pidof -x如果您知道进程名称或kill -0PID ,则可以使用。

Example:

例子:

if [ -f yourapp.lock ]; then
echo "The process is already launched, please wait..."
fi

回答by Boris Guéry

Why don't set a lock file ?

为什么不设置锁定文件?

Something like

就像是

yourapp.lock

yourapp.lock

Just remove it when you process is finished, and check for it before to launch it.

只需在处理完成后将其删除,并在启动之前检查它。

It could be done using

可以使用

lockfile=~/myproc.lock
minutes=60
if [ -f "$lockfile" ]
then
  filestr=`find $lockfile -mmin +$minutes -print`
  if [ "$filestr" = "" ]; then
    echo "Lockfile is not older than $minutes minutes! Another 
echo "Removing lock $lockfile ..."
rm $lockfile
running. Exiting ..." exit 1 else echo "Lockfile is older than $minutes minutes, ignoring it!" rm $lockfile fi fi echo "Creating lockfile $lockfile" touch $lockfile

回答by A1g0r1thm

This is how I do it in one of my cron jobs

这就是我在一项 cron 工作中的做法

pgrep -f yourscript >/dev/null && exit

and delete the lock file at the end of the script

并删除脚本末尾的锁定文件

echo "Starting process..."
run-process > $OUTPUT &
pid=$!
echo "Process started pid=$pid"
while true; do
    kill -0 $pid 2> /dev/null || { echo "Process exit detected"; break; }
    sleep 1
done
echo "Done."

回答by chaos

echo $$ > /tmp/lock.$$
if ! ln /tmp/lock.$$ /tmp/lock ; then 
        echo "previous backup in process"
        rm /tmp/lock.$$
        exit
fi

回答by lhunath

For a method that does not suffer from parsing bugs and race conditions, check out:

对于不受解析错误和竞争条件影响的方法,请查看:

回答by Ruslan

I had recently the same question and found from above that kill -0 is best for my case:

我最近遇到了同样的问题,从上面发现 kill -0 最适合我的情况:

FILE="/tmp/my_file"
if [ -f "$FILE" ]; then
   echo "Still running"
   exit
fi
trap EXIT "rm -f $FILE"
touch $FILE

...script here...

回答by Paul Tomblin

To expand on what @bgy says, the safe atomicway to create a lock file if it doesn't exist yet, and fail if it doesn't, is to create a temp file, then hard link it to the standard lock file. This protects against another process creating the file in between you testing for it and you creating it.

为了扩展@bgy 所说的内容,如果锁文件尚不存在,则创建锁文件的安全原子方法是创建临时文件,然后将其硬链接到标准锁文件。这可以防止在您测试文件和创建文件之间创建文件的另一个进程。

Here is the lock file code from my hourly backup script:

这是我的每小时备份脚本中的锁定文件代码:

##代码##

Don't forget to delete both the lock file and the temp file when you're done, even if you exit early through an error.

完成后不要忘记删除锁定文件和临时文件,即使您因错误提前退出。

回答by 0x6adb015

Use this script:

使用这个脚本:

##代码##

This script will create a file and remove it on exit.

此脚本将创建一个文件并在退出时将其删除。