Hi All ,

I have paste my script below for linux , i need someone to help me on how to add “while” which can loop the same command till the particular service is stopped properly and to start back the same services once the particular service stopped properly without any problem. I need to know if anyone can help me on this,

#!/bin/bash
SERVICE=“servicemix”
if ps -ef | grep -v grep |grep $SERVICE> /dev/null

do
echo “$SERVICE is running”
done

else
echo “$SERVICE stopped”
systemctl start servicemix

fi

5 Spice ups

For an infinite loop you need while true

#!/bin/bash
SERVICE="servicemix"
while true; do
  if ps -ef | grep -v grep |grep $SERVICE> /dev/null; then
    echo "$SERVICE is running"
  else
    echo "$SERVICE stopped"
    systemctl start servicemix 
  fi
done

… this however is ugly, you are constantly running three executables with no delay. This would be better…

#!/bin/bash
SERVICE=servicemix
PIDFILE=/var/run/${SERVICE}.pid
while true; do
  if [ -e ${PIDFILE} ]; then
    PID=`cat ${PIDFILE}`
    if kill -0 &>1 > /dev/null ${PID}; then
      echo "$SERVICE is running"
    else
      echo "$SERVICE stopped"
      systemctl start servicemix 
    fi
  fi;
  sleep 30;
 done

… this also has the advance of being able to shut down the service (which removes the pid file) without your script starting it back up again. If is crashes however the pid file will be left behind and the script will restart it.

HOWEVER!..

All this said, check out supervisor, it can usually be installed with a simple apt/yum install and is a much better tool for what you want to do :slight_smile:

Once installed you can simply place a config in /etc/supervisor/conf.d/ and have the daemon monitor a program for you :wink:

I hope this helps.

@privasiainventory

Will this script wait for the process to end fully since servicemix is a kind of sensitive , please advise.

#!/bin/bash

SERVICE=“servicemix”

while true; do if ps -ef | grep -v grep |grep $SERVICE> /dev/null; then

echo “$SERVICE is running”

else echo “$SERVICE stopped”

systemctl start servicemix

fi

done

Sorry, not really sure which script you are talking about. You may want to edit your post and tidy it up a bit.

This script is consistently running , is there a way to stop that and start back the services silently after stop the services.

#!/bin/bash SERVICE=“servicemix” while true; do if ps -ef | grep -v grep |grep $SERVICE> /dev/null; then echo “$SERVICE is running” else echo “$SERVICE stopped” systemctl start servicemix fi done

Add a “break

From the bash man page:

" break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater
than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless n is not greater than or
equal to 1.
"

1 Spice up

If you have systemd (which the use of sysctrl implies) why are you manually looking at pids?

Wouldn’t systemctl is-active application.service or systemctl is-failed application.service be more appropriate?

1 Spice up