Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Assistiance in small bash script

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-21-2013
Aviel.shani Aviel.shani is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 31 March 2014, 7:49 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
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 :

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


Last edited by Scott; 07-21-2013 at 02:32 AM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 07-21-2013
ddreggors's Unix or Linux Image
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
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...


Code:
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:


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

Which says if '$SERVICE' *is* equal to "xinetd is stopped" then restart.
Sponsored Links
    #3  
Old Unix and Linux 07-21-2013
Aviel.shani Aviel.shani is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 31 March 2014, 7:49 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the help, but it does not work .. Linux
It was also the first option that i tried to debug the script.
    #4  
Old Unix and Linux 07-21-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 29 May 2015, 3:53 PM EDT
Location: Amsterdam
Posts: 10,221
Thanks: 334
Thanked 2,700 Times in 2,416 Posts
You need something like:

Code:
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:

Code:
if ! $SERVICE status > /dev/null 2>&1
then

Sponsored Links
    #5  
Old Unix and Linux 07-21-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 29 May 2015, 2:09 PM EDT
Location: France
Posts: 319
Thanks: 19
Thanked 81 Times in 76 Posts

Code:
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):


Code:
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:


Code:
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

Sponsored Links
    #6  
Old Unix and Linux 07-22-2013
ddreggors's Unix or Linux Image
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
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.
Sponsored Links
    #7  
Old Unix and Linux 07-22-2013
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 29 May 2015, 2:09 PM EDT
Location: France
Posts: 319
Thanks: 19
Thanked 81 Times in 76 Posts
Actually, Scrutinizer's reply is way more simple to use Linux
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...
The Following User Says Thank You to tukuyomi For This Useful Post:
Scrutinizer (07-22-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Making a bash script and small C program for a homework assignment twk101 Shell Programming and Scripting 1 04-26-2013 02:22 AM
need a small script Rayzone Shell Programming and Scripting 1 04-09-2009 04:44 PM
A small project with bash script and craigslist hexpill Shell Programming and Scripting 0 11-25-2008 07:51 AM
small script help ali560045 Shell Programming and Scripting 9 01-18-2008 09:18 AM
bash script working for small size files but not for big size files. davidpreml Shell Programming and Scripting 1 11-01-2006 10:06 AM



All times are GMT -4. The time now is 04:20 PM.