问题描述
我的流程经常失败&有时会启动重复的实例..
I have a process that fails regularly & sometimes starts duplicate instances..
当我跑步时:ps x |grep -v grep |grep -c "processname"
我会得到:2
这是正常现象,因为该过程与恢复过程一起运行.
When I run:ps x |grep -v grep |grep -c "processname"
I will get:2
This is normal as the process runs with a recovery process..
如果我知道0
我将要开始这个过程如果我有:4
我将要停止&重新启动过程
If I get0
I will want to start the processif I get:4
I will want to stop & restart the process
我需要一种获取ps x |grep -v grep |grep -c "processname"
然后设置一个简单的3选项功能
Then setup a simple 3 option function
ps x |grep -v grep |grep -c "processname"
if answer = 0 (start process & write NOK & Time to log /var/processlog/check)
if answer = 2 (Do nothing & write OK & time to log /var/processlog/check)
if answer = 4 (stot & restart the process & write NOK & Time to log /var/processlog/check)
此过程停止killall -9 process
该过程开始于process -b -c /usr/local/etc
The process is stopped withkillall -9 process
The process is started withprocess -b -c /usr/local/etc
我的主要问题是找到一种对ps x |grep -v grep |grep -c "processname"
的结果采取行动的方法.
My main problem is finding a way to act on the result of ps x |grep -v grep |grep -c "processname"
.
理想情况下,我想使grep的结果成为脚本中的变量,如下所示:
Ideally, I would like to make the result of that grep a variable within the script with something like this:
process=$(ps x |grep -v grep |grep -c "processname")
如果可能的话.
推荐答案
脚本存储在crontab
中,每分钟运行一次.
Script is stored in crontab
and runs once every minute.
#! /bin/bash
case "$(pidof amadeus.x86 | wc -w)" in
0) echo "Restarting Amadeus: $(date)" >> /var/log/amadeus.txt
/etc/amadeus/amadeus.x86 &
;;
1) # all ok
;;
*) echo "Removed double Amadeus: $(date)" >> /var/log/amadeus.txt
kill $(pidof amadeus.x86 | awk '{print $1}')
;;
esac
0
如果找不到进程,请重新启动它.1
如果找到了进程,则一切正常.*
如果进程运行2个或更多,则杀死最后一个.
0
If process is not found, restart it.1
If process is found, all ok.*
If process running 2 or more, kill the last.
它只是测试pidof
程序中的退出标志$?
.将会是0
进程正在运行,如果不是1
.
It just tests the exit flag $?
from the pidof
program. It will be 0
of process is running and 1
if not.
#!/bin/bash
pidof amadeus.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
echo "Restarting Amadeus: $(date)" >> /var/log/amadeus.txt
/etc/amadeus/amadeus.x86 &
fi
最后是一支班轮
pidof amadeus.x86 >/dev/null ; [[ $? -ne 0 ]] && echo "Restarting Amadeus: $(date)" >> /var/log/amadeus.txt && /etc/amadeus/amadeus.x86 &
cccam oscam
cccam oscam
这篇关于Linux脚本,用于检查进程是否正在运行并根据结果采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!