Bash 脚本来检查运行过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2903354/
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
Bash script to check running process
提问by elasticsecurity
I wrote a bash-script to check if a process is running. It doesn't work since the ps command always returns exit code 1. When I run the ps command from the command-line, the $? is correctly set, but within the script it is always 1. Any idea?
我写了一个 bash 脚本来检查进程是否正在运行。它不起作用,因为 ps 命令总是返回退出代码 1。当我从命令行运行 ps 命令时,$? 设置正确,但在脚本中它始终为 1。知道吗?
#!/bin/bash
SERVICE=
ps -a | grep -v grep | grep > /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 version: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Bash 版本:GNU bash,版本 3.2.25(1)-release (x86_64-redhat-linux-gnu)
回答by Andor
There are a few really simple methods:
有几个非常简单的方法:
pgrep procname && echo Running
pgrep procname || echo Not running
killall -q -0 procname && echo Running
pidof procname && echo Running
回答by Anders
I tried your version on BASH version 3.2.29, worked fine. However, you could do something like the above suggested, an example here:
我在 BASH 版本 3.2.29 上尝试了您的版本,效果很好。但是,您可以执行上述建议的操作,例如此处:
#!/bin/sh
SERVICE=""
RESULT=`ps -ef | grep | grep -v 'grep' | grep -v #!/bin/bash
ps_out=`ps -ef | grep | grep -v 'grep' | grep -v 20:10 $ checkRunningProcess.sh proxy.py
Running
20:12 $ checkRunningProcess.sh abcdef
Not Running
`
result=$(echo $ps_out | grep "")
if [[ "$result" != "" ]];then
echo "Running"
else
echo "Not Running"
fi
`
result=$(echo $ps_out | grep "")
if [[ "$result" != "" ]];then
echo "Running"
else
echo "Not Running"
fi
回答by androidyue
This trick works for me. Hope this could help you. Let's save the followings as checkRunningProcess.sh
这个技巧对我有用。希望这可以帮助你。让我们将以下内容保存为 checkRunningProcess.sh
#!/bin/sh
PROCESS=""
PROCANDARGS=$*
while :
do
RESULT=`pgrep ${PROCESS}`
if [ "${RESULT:-null}" = null ]; then
echo "${PROCESS} not running, starting "$PROCANDARGS
$PROCANDARGS &
else
echo "running"
fi
sleep 10
done
Make the checkRunningProcess.sh executable.And then use it.
Example to use.
使 checkRunningProcess.sh 可执行。然后使用它。
使用示例。
#!/bin/sh
SERVICE=
if ps ax | grep -v grep | grep -v !/bin/bash
CHECK=#!/bin/bash
SERVICE=/path/to/my/service
result=$(ps ax|grep -v grep|grep $SERVICE)
echo ${#result}
if ${#result}> 0
then
echo " Working!"
else
echo "Not Working.....Restarting"
/usr/bin/xvfb-run -a /opt/python27/bin/python2.7 SERVICE &
fi
SERVICE=
DATE=`date`
OUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep )
echo $OUTPUT
if [ "${#OUTPUT}" -gt 0 ] ;
then echo "$DATE: $SERVICE service running, everything is fine"
else echo "$DATE: $SERVICE is not running"
fi
| grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi
回答by user2128898
I use this one to check every 10 seconds process is running and start if not and allows multiple arguments:
我用这个来检查每 10 秒进程是否正在运行,如果没有则启动并允许多个参数:
#!/bin/bash ps axho comm| grep > /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" /etc/init.d/ restart fi
回答by suda
Check if your scripts name doesn't contain $SERVICE. If it does, it will be shown in ps results, causing script to always think that service is running. You can grep it against current filename like this:
检查您的脚本名称是否不包含 $SERVICE。如果是,会在ps结果中显示,导致脚本一直认为服务正在运行。您可以像这样针对当前文件名对其进行 grep:
HTTPDSERVICE=$(ps -A | grep httpd | head -1)
[ -z "$HTTPDSERVICE" ] && echo "No apache service running."
回答by Dani Radulescu
Working one.
工作之一。
!/bin/bash
pgrep && echo Running
回答by Andrew Scott Evans
Despite some success with the /dev/null approach in bash. When I pushed the solution to cron it failed. Checking the size of a returned command worked perfectly though. The ampersrand allows bash to exit.
尽管在 bash 中使用 /dev/null 方法取得了一些成功。当我将解决方案推送到 cron 时,它失败了。不过,检查返回命令的大小工作得很好。&符号允许 bash 退出。
##代码##回答by Mike Veltman
Something like this
像这样的东西
回答by Mike
Those are helpful hints. I just needed to know if a service was running when I started the script, so I could leave the service in the same state when I left. I ended up using this:
这些都是有用的提示。当我启动脚本时,我只需要知道服务是否正在运行,这样我就可以在离开时让服务保持相同的状态。我最终使用了这个:
##代码##回答by CMP
A simple script version of one of Andor's above suggestions:
Andor 的上述建议之一的简单脚本版本:
##代码##If the above script is called test.sh then, in order to test, type: test.sh NameOfProcessToCheck
如果上面的脚本被称为 test.sh 那么,为了测试,输入:test.sh NameOfProcessToCheck
e.g. test.sh php
例如 test.sh php