通过脚本监控进程

利用命令pgrep可以看一个进程是否存在

例子:

#-------------------/chapter13/ex13-9.sh-------------------

#! /bin/bash

#Apache httpd 进程监控shell

#启动服务命令

RSSTART="/sbin/service httpd start"

#pgrep 命令路径

PGREP="/usr/bin/pgrep"

#Apache Web 服务器的进程名称

HTTPD="httpd"

#查找httpd进程,&>/dev/null 是把输出到标准输入和标准错误的文本忽略掉。

$PGREP  ${HTPPD} &>/dev/null

#如果没有找到,则重新启动服务

if [ $? -ne 0 ]

then 

   $RESTART

fi

定期运行上面脚本,

*/30 * * * *  /root/chapter13/ex13-9.sh > /dev/null 2>&1.

注:pgrep httpd,若进程httpd存在,则$?=0,如果不存在,则$?!=0

做个小推广:程序员经常久坐,颈椎毛病比较多,特别推荐ventry颈椎保健枕

原文地址:https://www.cnblogs.com/longzhongren/p/4251404.html