用shell脚本实现每隔30s检查httpd进程存在与否,httpd存在时输出0,不存在输出1.方法一:单条命令实现catapache.sh#!/bin/bashwhiletruedops-ef|grephttp|grep-vgrep>/dev/null&&echo0||echo1sleep30donewhiletrue为真,一直执行do循环。#ps-ef|grephttp,过滤出http进程输出结果:root72861015:14?00:00:00/usr/sbin/httpdnagios72887286015:14?00:00:00/usr/sbin/httpdnagios72897286015:14?00:00:00/usr/sbin/httpdnagios72907286015:14?00:00:00/usr/sbin/httpdnagios72917286015:14?00:00:00/usr/sbin/httpdnagios72927286015:14?00:00:00/usr/sbin/httpdnagios72937286015:14?00:00:00/usr/sbin/httpdnagios72947286015:14?00:00:00/usr/sbin/httpdnagios72957286015:14?00:00:00/usr/sbin/httpdroot74404708015:17pts/000:00:00grephttp#ps-ef|grephttp|grep-vgrep,过滤ps-ef|grephttp本身。输出结果:root72861015:14?00:00:00/usr/sbin/httpdnagios72887286015:14?00:00:00/usr/sbin/httpdnagios72897286015:14?00:00:00/usr/sbin/httpdnagios72907286015:14?00:00:00/usr/sbin/httpdnagios72917286015:14?00:00:00/usr/sbin/httpdnagios72927286015:14?00:00:00/usr/sbin/httpdnagios72937286015:14?00:00:00/usr/sbin/httpdnagios72947286015:14?00:00:00/usr/sbin/httpdnagios72957286015:14?00:00:00/usr/sbin/httpd#ps-ef|grephttp|grep-vgrep>/dev/null,输出到空设备文件。#ps-ef|grephttp|grep-vgrep>/dev/null&&echo0||echo1逻辑与:&&,逻辑或:||。"ps-ef|grephttp|grep-vgrep>/dev/null"为真时执行echo0,否则执行echo1.方法二:catapache.shwhiletruehttpnum=`ps-ef|grephttp|grep-vgrep|wc-l`doif[$httpnum-gt0]thenecho0elseecho1fisleep30done方案二摘自老男孩博客http://oldboy.blog.51cto.com/2561410/577227,里面有详细介绍。