Linux的脚本来检查,如果进程正在运行和放大器;行为上的结果 [英] Linux Script to check if process is running & act on the result
问题描述
我有定期和放大器出现故障的过程;有时开始重复的实例。
I have a process that fails regularly & sometimes starts duplicate instances..
当我运行: PS X | grep的-v grep的| grep的-cprocessname
我会得到: 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的-cprocessname
然后设置一个简单的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进程
该过程开始过程-b -c在/ usr / local / etc中
我的主要问题是寻找一种方法来作用于 PS x的结果| grep的-v grep的|。grep的-cprocessname
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:
程序= $(ps的X | grep的-v grep的| grep的-cprocessname)
如果可能的。
推荐答案
下面是一个脚本,我使用的,如果系统上的进程正在运行进行监控。结果
脚本存储在的crontab
和每分钟运行一次。
Here is a script I use to monitor if a process on a system is running.
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
如果没有
A simpler version. This just test if process is running, and if not restart it.
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
这篇关于Linux的脚本来检查,如果进程正在运行和放大器;行为上的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!