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

当我 run 时:

如果我得到

我需要的是一种计算ps x |grep -v grep |grep -c "processname"的方法

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

该过程被停止

我的主要问题是如何根据ps x |grep -v grep |grep -c "processname"的结果采取行动.

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

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

如果可能的话.

推荐答案

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

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

This works with if process is not running or process is running multiple times:

#! /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 If process is not found, restart it.
1 If process is found, all ok.
* If process running 2 or more, kill the last.


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

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

然后可以在crontab中使用它,每分钟运行一次,如下所示:

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

cccam oscam

Linux相关问答推荐

无法在Raspberry PI 3 Model B上分配256TB的虚拟内存

Aarch64在Linux上是否有红色区域,如果有,是16个字节还是128个字节?

在Zenity进度窗口上单击取消后如何停止bash脚本

如何在带模式的文件频繁更改的管道中使用grep-f带模式的文件?

Azure Linux B1s VM-Jenkins Sever已安装,但主页未打开

Linux 的 __fastfail 替代方案?

使用awk命令将以:分隔的两个文件合并的方法

从另一个文件中的大文件中查找行的最快方法

Bash 更新 yaml 文件中的图像值

当未在日志(log)中输入确切的时间戳时如何过滤日期范围内的值

进程Forking 后 pthread_key_create() 生成的密钥会发生什么?

如何使用该位置的相对路径在单个位置创建多个文件夹?

为什么 mmap() 比顺序 IO 快?

'&&' 与 '&' 与 Bash 中的 'test' 命令

用于提取 IP 地址的 Linux bash 脚本

当父进程被杀死时,使用 fork() 创建的子进程是否会自动被杀死?

在 shell 脚本的 for 循环中迭代行而不是单词

Vim 增量搜索

在我的 index.php 中加载 CSS 和 JS 等资源时出现错误 403

Linux 中合理数量的 inotify 监视是多少?