Linux脚本检查进程是否正在运行并根据结果采取行动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20162678/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:26:35  来源:igfitidea点击:

Linux Script to check if process is running and act on the result

linuxbashprocessgrep

提问by linuxnoob

I have a process that fails regularly & sometimes starts duplicate instances..

我有一个经常失败的过程,有时会启动重复的实例..

When I run: ps x |grep -v grep |grep -c "processname"I will get: 2This is normal as the process runs with a recovery process..

当我运行时: ps x |grep -v grep |grep -c "processname"我会得到: 2这是正常的,因为该进程在一个恢复进程中运行..

If I get 0I will want to start the process if I get: 4I will want to stop & restart the process

如果我得到 0我想开始这个过程如果我得到: 4我想停止并重新启动这个过程

What I need is a way of taking the result of ps x |grep -v grep |grep -c "processname"

我需要的是一种获取结果的方法 ps x |grep -v grep |grep -c "processname"

Then setup a simple 3 option function

然后设置一个简单的 3 选项功能

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)

The process is stopped with killall -9 processThe process is started with process -b -c /usr/local/etc

进程停止于 killall -9 process进程启动于 process -b -c /usr/local/etc

My main problem is finding a way to act on the result of ps x |grep -v grep |grep -c "processname".

我的主要问题是找到一种对ps x |grep -v grep |grep -c "processname".

Ideally, I would like to make the result of that grep a variable within the script with something like this:

理想情况下,我想让 grep 的结果成为脚本中的一个变量,如下所示:

process=$(ps x |grep -v grep |grep -c "processname")

process=$(ps x |grep -v grep |grep -c "processname")

If possible.

如果可能的话。

采纳答案by Jotne

Programs to monitor if a process on a system is running.

用于监视系统上的进程是否正在运行的程序。

Script is stored in crontaband runs once every minute.

脚本存储crontab并每分钟运行一次。

This works with not running process an to many process:

这适用于不运行进程和多个进程:

#! /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 }')
    ;;
esac

0If process is not found, restart it.
1If process is found, all ok.
*If process running 2 or more, kill the last.

0如果找不到进程,请重新启动它。
1如果找到进程,则一切正常。
*如果进程运行 2 个或更多,则杀死最后一个。



A simpler version. This just test if process is running, and if not restart it.

一个更简单的版本。这只是测试进程是否正在运行,如果没有重新启动它。

It just tests the exit flag $?from the pidofprogram. It will be 0of process is running and 1if not.

它只是测试出口标志$?pidof程序。它将是0进程正在运行,1如果没有。

#!/bin/bash
pidof  amadeus.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt
        /etc/amadeus/amadeus.x86 &
fi


And at last, a one liner

最后,一个单班轮

pidof amadeus.x86 >/dev/null ; [[ $? -ne 0 ]] && echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt && /etc/amadeus/amadeus.x86 &

cccam oscam

cccam oscam

回答by linuxnoob

I have adopted your script for my situation Jotne.

我已经根据我的情况采用了你的脚本 Jotne。

#! /bin/bash

logfile="/var/oscamlog/oscam1check.log"

case "$(pidof oscam1 | wc -w)" in

0)  echo "oscam1 not running, restarting oscam1:     $(date)" >> $logfile
    /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1 &
    ;;
2)  echo "oscam1 running, all OK:     $(date)" >> $logfile
    ;;
*)  echo "multiple instances of oscam1 running. Stopping & restarting oscam1:     $(date)" >> $logfile
    kill $(pidof oscam1 | awk '{print }')
    ;;
esac

While I was testing, I ran into a problem.. I started 3 extra process's of oscam1 with this line: /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1which left me with 8 process for oscam1. the problem is this.. When I run the script, It only kills 2 process's at a time, so I would have to run it 3 times to get it down to 2 process..

在我测试时,我遇到了一个问题。我用这一行启动了 oscam1 的 3 个额外进程: /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1这给我留下了 8 个 oscam1 进程。问题是这样的..当我运行脚本时,它一次只杀死 2 个进程,所以我必须运行 3 次才能将它降到 2 个进程。

Other than killall -9 oscam1followed by /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1, in *)is there any better way to killall apart from the original process? So there would be zero downtime?

除了killall -9 oscam1其次/usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1,在*)有没有更好的办法从原来的进程killall分开?那么会有零停机时间吗?

回答by Tirias

I adopted the @Jotne solution and works perfectly! For example for mongodb server in my NAS

我采用了@Jotne 解决方案并且完美运行!例如我的 NAS 中的 mongodb 服务器

#! /bin/bash

case "$(pidof mongod | wc -w)" in

0)  echo "Restarting mongod:"
    mongod --config mongodb.conf
    ;;
1)  echo "mongod already running"
    ;;
esac

回答by Kris Long

If you changed awk '{print $1}' to '{ $1=""; print $0}' you will get all processes except for the first as a result. It will start with the field separator (a space generally) but I don't recall killall caring. So:

如果您将 awk '{print $1}' 更改为 '{ $1=""; 打印 $0}' 您将获得除第一个之外的所有进程作为结果。它将以字段分隔符(通常为空格)开头,但我不记得 killall 关心。所以:

#! /bin/bash

logfile="/var/oscamlog/oscam1check.log"

case "$(pidof oscam1 | wc -w)" in

0)  echo "oscam1 not running, restarting oscam1:     $(date)" >> $logfile
    /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1 &
    ;;
2)  echo "oscam1 running, all OK:     $(date)" >> $logfile
    ;;
*)  echo "multiple instances of oscam1 running. Stopping & restarting oscam1:     $(date)" >> $logfile
    kill $(pidof oscam1 | awk '{ =""; print ##代码##}')
    ;;
esac

It is worth noting that the pidof route seems to work fine for commands that have no spaces, but you would probably want to go back to a ps-based string if you were looking for, say, a python script named myscript that showed up under ps like

值得注意的是,pidof 路由似乎适用于没有空格的命令,但如果您正在寻找一个名为 myscript 的 python 脚本,您可能希望返回到基于 ps 的字符串,该脚本出现在ps 喜欢

root 22415 54.0 0.4 89116 79076 pts/1 S 16:40 0:00 /usr/bin/python /usr/bin/myscript

根 22415 54.0 0.4 89116 79076 pts/1 S 16:40 0:00 /usr/bin/python /usr/bin/myscript

Just an FYI

仅供参考