gpt4 book ai didi

用于检查进程是否正在运行并对结果采取行动的 Linux 脚本

转载 作者:IT老高 更新时间:2023-10-28 12:40:03 25 4
gpt4 key购买 nike

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

当我运行时:ps x |grep -v grep |grep -c "进程名"我会得到:2这是正常的,因为该过程会以恢复过程运行。

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

我需要的是一种获取 ps x |grep -v grep |grep -c "p​​rocessname"

的结果的方法

然后设置一个简单的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)

进程停止killall -9 进程该过程开始于process -b -c/usr/local/etc

我的主要问题是找到一种方法来处理 ps x |grep -v grep |grep -c "p​​rocessname" 的结果。

理想情况下,我想将该 grep 的结果作为脚本中的变量,如下所示:

process=$(ps x |grep -v grep |grep -c "p​​rocessname")

如果可能的话。

最佳答案

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

脚本存储在 crontab 中,每分钟运行一次。

这适用于进程未运行或进程多次运行的情况:

#! /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 个或更多,杀死最后一个。


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

它只是测试 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

最后,一个类轮

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

然后可以在 crontab 中使用它来像这样每分钟运行一次:

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

cccam oscam

关于用于检查进程是否正在运行并对结果采取行动的 Linux 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com