Sunday, April 3, 2022

[SOLVED] shell script to perform stop start multiple httpd instances

Issue

I want to write a script to do restart of httpd instances only if it is in running status. For ine instance it is working fine, but more than one instance it is failing.

below is script which I am using:

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

Please suggest...


Solution

You mentioned there are multiple instances, i see it misses for loop on execution of script. Here it only restarts the last one picked in the $ctl_var

Modified script should look something like below, tweak script if necessary :

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

Hope this helps.



Answered By - Giri
Answer Checked By - Mary Flores (WPSolving Volunteer)