我编写了一个bash脚本来检查进程是否正在运行。它不起作用,因为ps命令总是返回退出代码1。当我从命令行运行ps命令时,$?是正确设置的,但在脚本中总是为1。你知道吗?
#!/bin/bash
SERVICE=$1
ps -a | grep -v grep | grep $1 > /dev/null
result=$?
echo "exit code: ${result}"
if [ "${result}" -eq "0" ] ; then
echo "`date`: $SERVICE service running, everything is fine"
else
echo "`date`: $SERVICE is not running"
fi
Bash版本: GNU bash,版本3.2.25(1)-release (x86_64-redhat-linux-gnu)
发布于 2011-06-03 18:13:07
有几种非常简单的方法:
pgrep procname && echo Running
pgrep procname || echo Not running
killall -q -0 procname && echo Running
pidof procname && echo Running
发布于 2010-05-25 17:54:05
我在BASH版本3.2.29上试用了你的版本,运行良好。但是,您可以执行类似上述建议的操作,下面是一个示例:
#!/bin/sh
SERVICE="$1"
RESULT=`ps -ef | grep $1 | grep -v 'grep' | grep -v $0`
result=$(echo $ps_out | grep "$1")
if [[ "$result" != "" ]];then
echo "Running"
else
echo "Not Running"
fi
发布于 2014-06-10 20:14:14
这个技巧对我很有效。希望这能对你有所帮助。让我们将以下代码保存为checkRunningProcess.sh
#!/bin/bash
ps_out=`ps -ef | grep $1 | grep -v 'grep' | grep -v $0`
result=$(echo $ps_out | grep "$1")
if [[ "$result" != "" ]];then
echo "Running"
else
echo "Not Running"
fi
创建checkRunningProcess.sh executable.And,然后使用它。
要使用的示例。
20:10 $ checkRunningProcess.sh proxy.py
Running
20:12 $ checkRunningProcess.sh abcdef
Not Running
https://stackoverflow.com/questions/2903354
复制相似问题