Assistiance in small bash script

Hi All,
i wrote an simple script, that will monitor some services ,and if particular service will found down ,the code will restarts the service and message about the process will be sent on mail.

I have a problem somewhere in the syntax that the script initializes the service even if is up ..

Your help please, what should I fix the code :

#!/bin/bash
# running. If its not it is restarted and an email is sent to
# the configured mailbox.
SERVICE=/etc/init.d/xinetd
MAILBOX=*********
STATUS="/etc/init.d/xinetd status"
if [ "$SERVICE status" != 'xinetd is stopped' ]
then
  /etc/init.d/xinetd restart
  $STATUS | mail -s "the xinet.d service getting restart" $MAILBOX
fi
exit 0

If I read this correctly, you only want to restart the service if it is stopped.

If that is the case then your issue is the if statement logic...

if [ "$SERVICE status" != 'xinetd is stopped' ]

Which says if '$SERVICE' is not equal "xinetd is stopped" then restart xinetd...

I believe what you are looking for is:

if [ "$SERVICE status" = 'xinetd is stopped' ]

Which says if '$SERVICE' *is* equal to "xinetd is stopped" then restart.

Thanks for the help, but it does not work .. :slight_smile:
It was also the first option that i tried to debug the script.

You need something like:

if [ "$($SERVICE status)" =  "some status string" ])
then

to compare the textual status output of the xinetd stop/start script...

If the xinetd stop / start script outputs a return code; you could try something like:

if ! $SERVICE status > /dev/null 2>&1
then
if [ "$SERVICE status" = 'xinetd is stopped' ]

In this case, "$SERVICE status" is equal to "/etc/init.d/xinetd status" , not 'xinetd is stopped'

I suggest you to use exit code instead of what is outputed. (I do not have xinetd on my machine, so I'll use bind in this example):

unix.com# /etc/init.d/bind9 start
[ ok ] Starting domain name service...: bind9.

unix.com# /etc/init.d/bind9 status
[ ok ] bind9 is running.

unix.com# echo $?
0

unix.com# /etc/init.d/bind9 stop
[ ok ] Stopping domain name service...: bind9

unix.com# /etc/init.d/bind9 status
[FAIL] bind9 is not running ... failed!

unix.com# echo $?
3

In your script, you do:

SERVICE=/etc/init.d/xinetd
MAILBOX=*********
STATUS="/etc/init.d/xinetd status"

$SERVICE status > /dev/null
x="$?"
echo "Exit Code is $x"
if [ "$x" -eq ...whatever it is when NOT running ]; then 
Restart the service
Continue your script here

tukuyomi is correct, I did not even notice that you were using "$SERVICE status"... for some reason I thought I saw "$STATUS" which would have been more correct as it contained the output from $($SERVICE status).

Yet, even more correct (as tukuyomi pointed out) would have been the exit code from the service command vs the resulting text output from the service command.

Actually, Scrutinizer's reply is way more simple to use :slight_smile:
if ! $SERVICE status > /dev/null 2>&1 uses exit status as well; in this case if $SERVICE status did not ( ! ) exit with status 0; then rest of the code...

1 Like