使用 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
Check if a process is running using Bash
提问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
回答by anubhava
For systems where pgrep
isn'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 forcegrep
to not list itself as it won't match[.]
regex.grep -q
can be utilized to avoid command substitution step.- Prefer using lowercase variables in shell.
[.]
in 将强制grep
不列出自己,因为它不匹配[.]
正则表达式。grep -q
可用于避免命令替换步骤。- 更喜欢在 shell 中使用小写变量。
回答by myaut
The problem is that grep
you call sometimes finds himself in a ps
list, 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.d
start scripts.
这是/etc/init.d
启动脚本中的常用技术。
回答by John Schmitt
回答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 lxpanel
or pidof lxpanel
still reports that lxpanel
is running even when it is defunct (zombie); so to get alive-and-running process, we need to use ps
and grep
注意:pgrep -x lxpanel
或者即使它已经不存在(僵尸)pidof lxpanel
仍然报告lxpanel
正在运行;所以为了获得活跃和运行的过程,我们需要使用ps
和grep
回答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