Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2039924
  • 博文数量: 470
  • 博客积分: 10206
  • 博客等级: 上将
  • 技术积分: 5620
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-03 12:50
文章分类

全部博文(470)

文章存档

2012年(1)

2011年(18)

2010年(47)

2009年(404)

分类:

2010-07-25 22:57:23

[] [] [] []

求助,,,

一个shell编程中的循环问题。。

例如一直监控httpd服务,,,查看服务用ps aux|grep httpd.

实现目标。在脚本中使用循环语句一直监控httpd服务,,如果发现httpd服务down了,,立即启动/etc/init.d/heartbeat。。我了解到循环语句有for,while等,,但参数不知道是什么,,,求各位大侠了啊~~~


|  |  | 

本帖最后由 mgqw 于 2010-07-23 11:07 编辑
  1. #!/bin/sh

  2. while [ 1 ]
  3. do
  4.     a=$(pgrep httpd)
  5.     if [ -z $a ]
  6.     then
  7.         /etc/init.d /heartbeat
  8.     else
  9.         echo "The httpd is running"
  10.     fi
  11.     sleep 1
  12. done
复制代码


|  |  | 

这个我试过了

while [ $program ]

do 

  echo :"The httpd is running"

done

但不行,,关键是[  ] 里面的参数变成$program后还有那些参数需要添加???


|  |  | 

回复  ccie-li 


    全部的程序都给你写出来了, 你的思路有问题


|  |  | 

本帖最后由 mgqw 于 2010-07-23 11:07 编辑

回复  ccie-li 


    或者是这样也行
  1. #!/bin/sh

  2. while [ 1 ]
  3. do
  4.     pgrep httpd
  5.     if [ $? -eq 1 ]
  6.     then
  7.         /etc/init.d /heartbeat
  8.     else
  9.         echo "The httpd is running"
  10.     fi
  11.     sleep 1
  12. done
复制代码


|  |  | 


放到cronjob中
  1. #! /usr/bin/bash

  2. if [[ `/usr/ucb/ps aux|grep httpd|grep -v grep` ]] ; then
  3.    echo "fine"
  4. else
  5.     echo /etc/init.d/heartbeat
  6. fi
复制代码
成功后,去掉第二个echo.
其实我以前更喜欢用pidof  功能和pgrep一模一样, 唯一的区别就是 pidof必须要有root权限和root的环境变量才能执行

阅读(1166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~