Shell脚本执行停止启动多个httpd实例

时间:2018-06-28 03:43:48

标签: apache shell httpd.conf

我想编写一个脚本来仅在httpd实例处于运行状态时重新启动它。对于ine实例,它工作正常,但有多个实例失败。

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

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

请建议...

1 个答案:

答案 0 :(得分:0)

您提到有多个实例,我看到它在执行脚本时错过了循环。在这里,它仅重新启动在$ 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

希望这会有所帮助。