首页 > 解决方案 > 执行停止启动多个 httpd 实例的 shell 脚本

问题描述

我想编写一个脚本来重新启动 httpd 实例,前提是它处于运行状态。对于一个实例,它工作正常,但不止一个实例它失败了。

下面是我正在使用的脚本:

ctl_var=`find /opt/apache/instances/ -name apachectl  | grep -v "\/httpd\/"`

ctl_proc=`ps -ef | grep -i httpd | grep -i " 1 " wc -l`

if [ $ctl_proc <= 0 ]; 
  then echo "httpd is not running"; 
  else $ctl_var -k stop; echo "httpd stopped successfully" ;
  sleep 5;
  $ctl_var -k start;
  sleep 5;
  echo "httpd started" ps -ef | grep httpd | grep -i " 1 "; 
fi

请建议...

标签: apacheshellhttpd.conf

解决方案


你提到有多个实例,我看到它在执行脚本时错过了循环。在这里它只重新启动 $ctl_var 中选择的最后一个

修改后的脚本应如下所示,必要时调整脚本:

ctl_var=`find /opt/apache/instances/ -name apachectl  | grep -v "\/httpd\/"`
ctl_proc=`ps -ef | grep -i httpd | grep -i " 1 " wc -l`

for i in `echo $ctl_var`
do
    if [ $ctl_proc <= 0 ]; 
      then echo "httpd is not running"; 
      else $i -k stop; echo "httpd stopped successfully" ;
      sleep 5;
      $i -k start;
      sleep 5;
      echo "httpd started" ps -ef | grep httpd | grep -i " 1 "; 
    fi
done

希望这可以帮助。


推荐阅读