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

shell script to perform stop start multiple httpd instances

我想编写一个脚本来仅在处于 运行 状态时才重启 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

请推荐...

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

希望对您有所帮助。