Hi All ,<\/p>\n
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,<\/p>\n
#!/bin/bash<\/strong> do<\/strong> else<\/strong> fi<\/strong><\/p>","upvoteCount":5,"answerCount":7,"datePublished":"2019-08-19T03:02:08.000Z","author":{"@type":"Person","name":"privasiainventory","url":"https://community.spiceworks.com/u/privasiainventory"},"suggestedAnswer":[{"@type":"Answer","text":" Hi All ,<\/p>\n 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,<\/p>\n #!/bin/bash<\/strong> do<\/strong> else<\/strong> fi<\/strong><\/p>","upvoteCount":5,"datePublished":"2019-08-19T03:02:08.000Z","url":"https://community.spiceworks.com/t/how-to-use-while-statement-for-bash-script/726069/1","author":{"@type":"Person","name":"privasiainventory","url":"https://community.spiceworks.com/u/privasiainventory"}},{"@type":"Answer","text":" For an infinite loop you need … this however is ugly, you are constantly running three executables with no delay. This would be better…<\/p>\n … 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.<\/p>\n HOWEVER!..<\/p>\n All this said, check out Once installed you can simply place a config in I hope this helps.<\/p>\n
\nSERVICE=“servicemix”<\/strong>
\nif ps -ef | grep -v grep |grep $SERVICE> /dev/null<\/strong><\/p>\n
\necho “$SERVICE is running”<\/strong>
\ndone<\/strong><\/p>\n
\necho “$SERVICE stopped”<\/strong>
\nsystemctl start servicemix<\/strong><\/p>\n
\nSERVICE=“servicemix”<\/strong>
\nif ps -ef | grep -v grep |grep $SERVICE> /dev/null<\/strong><\/p>\n
\necho “$SERVICE is running”<\/strong>
\ndone<\/strong><\/p>\n
\necho “$SERVICE stopped”<\/strong>
\nsystemctl start servicemix<\/strong><\/p>\nwhile true<\/code>…<\/p>\n
#!/bin/bash\nSERVICE=\"servicemix\"\nwhile true; do\n if ps -ef | grep -v grep |grep $SERVICE> /dev/null; then\n echo \"$SERVICE is running\"\n else\n echo \"$SERVICE stopped\"\n systemctl start servicemix \n fi\ndone\n<\/code><\/pre>\n
#!/bin/bash\nSERVICE=servicemix\nPIDFILE=/var/run/${SERVICE}.pid\nwhile true; do\n if [ -e ${PIDFILE} ]; then\n PID=`cat ${PIDFILE}`\n if kill -0 &>1 > /dev/null ${PID}; then\n echo \"$SERVICE is running\"\n else\n echo \"$SERVICE stopped\"\n systemctl start servicemix \n fi\n fi;\n sleep 30;\n done\n\n<\/code><\/pre>\n
supervisor<\/code>, it can usually be installed with a simple apt/yum install and is a much better tool for what you want to do
<\/p>\n
/etc/supervisor/conf.d/<\/code> and have the daemon monitor a program for you
<\/p>\n