使用 Bash 检查进程是否正在运行

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

Check if a process is running using Bash

linuxbashraspberry-pi

提问by myaut

I am trying to check if a process is running with the code below:

我正在尝试使用以下代码检查进程是否正在运行:

SERVICE="./yowsup/yowsup-cli"
RESULT=`ps aux | grep $SERVICE`

if [ "${RESULT:-null}" = null ]; then
    echo "not running"
else
    echo "running"
fi

But it keeps echoing it is running although it is not. I realized that the grep itself comes as a result and that is the issue.

但它不断回应它正在运行,尽管它不是。我意识到 grep 本身就是结果,这就是问题所在。

How can I skip the grep and just check for the process?

如何跳过 grep 并只检查进程?

回答by hek2mgl

Use pgrep:

使用pgrep

if pgrep "$SERVICE" >/dev/null 2>&1 ; then
    echo "$SERVICE is running"
fi

or, more reliable:

或者,更可靠:

if pgrep -f "/path/to/$SERVICE" >/dev/null 2>&1 ; then
    echo "$SERVICE is running"
fi

回答by anubhava

For systems where pgrepisn't available you can use:

对于pgrep不可用的系统,您可以使用:

service="[.]/yowsup/yowsup-cli"

if ps aux | grep -q "$service"; then
    echo "not running"
else
    echo "running"
fi
  • [.]in will force grepto not list itself as it won't match [.]regex.
  • grep -qcan be utilized to avoid command substitution step.
  • Prefer using lowercase variables in shell.
  • [.]in 将强制grep不列出自己,因为它不匹配[.]正则表达式。
  • grep -q可用于避免命令替换步骤。
  • 更喜欢在 shell 中使用小写变量。

回答by myaut

The problem is that grepyou call sometimes finds himself in a pslist, so it is good only when you check it interactively:

问题是grep你调用有时会发现自己在一个ps列表中,所以只有当你交互式地检查它时才好:

$ ps -ef | grep bash
...
myaut    19193  2332  0 17:28 pts/11   00:00:00 /bin/bash
myaut    19853 15963  0 19:10 pts/6    00:00:00 grep --color=auto bash

Easiest way to get it is to use pidof. It accepts both full path and executable name:

获得它的最简单方法是使用pidof. 它接受完整路径和可执行文件名称:

service="./yowsup/yowsup-cli" # or service="yowsup-cli"
if pidof "$service" >/dev/null; then
    echo "not running"
else
    echo "running"
fi

There is more powerful version of pidof-- pgrep.

有更强大的版本pidof-- pgrep



However, if you start your program from a script, you may save it's PID to a file:

但是,如果您从脚本启动程序,则可以将其 PID 保存到文件中:

service="./yowsup/yowsup-cli"
pidfile="./yowsup/yowsup-cli.pid"
service &
pid=$!
echo $pid > $pidfile

And then check it with pgrep:

然后检查它pgrep

if pgrep -F "$pidfile" >/dev/null; then
    echo "not running"
else
    echo "running"
fi

This is common technique in /etc/init.dstart scripts.

这是/etc/init.d启动脚本中的常用技术。

回答by John Schmitt

I thought pidofwas made for this.

我认为pidof是为此而设计的。

function isrunning()
{
    pidof -s "" > /dev/null 2>&1
    status=$?
    if [[ "$status" -eq 0 ]]; then
        echo 1
    else
        echo 0
    fi
)
if [[ $(isrunning bash) -eq 1 ]]; then echo "bash is running"; fi
if [[ $(isrunning foo) -eq 1 ]]; then echo "foo is running"; fi

回答by Bach Lien

## bash

## function to check if a process is alive and running:

_isRunning() {
    ps -o comm= -C "" 2>/dev/null | grep -x "" >/dev/null 2>&1
}

## example 1: checking if "gedit" is running

if _isRunning gedit; then
    echo "gedit is running"
else
    echo "gedit is not running"
fi

## example 2: start lxpanel if it is not there

if ! _isRunning lxpanel; then
    lxpanel &
fi

## or

_isRunning lxpanel || (lxpanel &)


Note: pgrep -x lxpanelor pidof lxpanelstill reports that lxpanelis running even when it is defunct (zombie); so to get alive-and-running process, we need to use psand grep

注意:pgrep -x lxpanel或者即使它已经不存在(僵尸)pidof lxpanel仍然报告lxpanel正在运行;所以为了获得活跃和运行的过程,我们需要使用psgrep

回答by Cristian

current_pid="$$" # get current pid
# Looking for current pid. Don't save lines either grep or current_pid
isRunning=$(ps -fea | grep -i $current_pid | grep -v -e grep -e $current_pid)

# Check if this script is running
if [[ -n "$isRunning" ]]; then
    echo "This script is already running."
fi

回答by Jumbo

SERVICE="./yowsup/yowsup-cli"
RESULT=`ps aux | grep $SERVICE|grep -v grep`

if [ "${RESULT:-null}" = null ]; then
    echo "not running"
else
    echo "running"
fi