欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

一个shell for循环与case结合的脚本(监控程序状态)

程序员文章站 2023-11-29 23:58:28
核心代码: 复制代码 代码如下:#/bin/bashset -xhosts="nginx mysql php-cgi"for myhost in $hosts ...

核心代码:

复制代码 代码如下:

#/bin/bash
set -x
hosts="nginx mysql php-cgi"
for myhost in $hosts
  do
  count=(`ps aux |grep $myhost |grep -v grep |wc -l`)
  echo "$myhost"
  echo "$count"

if [ $count -eq 0 ]; then
  case $myhost in
  nginx)
  cd /usr/local/webserver/nginx/sbin/
  ./nginx
  echo "nginx has be down"
  sleep 5

  mysql)
  /etc/init.d/mysqld start
  echo "mysql has be down"

  *)
  echo "what‘s the hell?"

esac
  fi
done
set +x