用于检查进程是否正在运行并根据结果采取行动的 Linux 脚本 [英] Linux Script to check if process is running and act on the result
问题描述
我有一个经常失败的流程&有时会启动重复的实例..
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 get
0
I will want to start the process
if 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 -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
如果找到进程,一切OK.*
如果进程运行 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 &
然后可以在 crontab 中使用它来像这样每分钟运行一次:
This can then be used in crontab to run every minute like this:
* * * * * pidof amadeus.x86 >/dev/null ; [[ $? -ne 0 ]] && echo "Restarting Amadeus: $(date)" >> /var/log/amadeus.txt && /etc/amadeus/amadeus.x86 &
cccam oscam
cccam oscam
这篇关于用于检查进程是否正在运行并根据结果采取行动的 Linux 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!