Linux check process and restart if it's stopped.

Debian5, Tomcat5.5, Java 1.6.6_0
I've a JAVA app that works with Tomcat5.5 and once or twice a day the Tomcat process stops and I have to restart it like this /etc/init.d/tomcat5.5 start
I've found this script but when I run it this is what I get back.  What's up with this???
"Usage: /etc/init.d/tomcat5.5 {start|stop|restart|try-restart|force-reload|status}."

I did make some mods to the script, the original is at the end of this message.
#!/bin/bash
pidof  jsvc >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting tomcat5.5:     $(date)" >> /var/log/tomcat.txt
        /etc/init.d/tomcat5.5 &
fi
----------------------------------
I've tried changing the syntax as in these examples.  No luck.
#!/bin/bash
pidof  jsvc >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting tomcat5.5:     $(date)" >> /var/log/tomcat.txt
        /etc/init.d/tomcat5.5 start &
fi

#!/bin/bash
pidof  jsvc >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting tomcat5.5:     $(date)" >> /var/log/tomcat.txt
        /etc/init.d/tomcat5.5 {start}
fi
---------------------------------------------------------

I found the script here and this is the original.
http://stackoverflow.com/questions/20162678/linux-script-to-check-if-process-is-running-act-on-the-result
#!/bin/bash
pidof  amadeus.x86 >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt
        /etc/amadeus/amadeus.x86 &
fi
------------------------------------------------
I'm aware of monit but I'd like to know where I'm going wrong in the shell script.  

Thanks
mobotAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simon3270Commented:
The first change, having

     /etc/init.d/tomcat5.5 start

is the right fix, but you don't need the & after it - the call will return after it has started the tomcat process.

What was the problem with this version?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mobotAuthor Commented:
Here's the script now.  Following your suggestion I've removed the &.

#!/bin/bash
pidof  jsvc >/dev/null
if [[ $? -ne 0 ]] ; then
        echo "Restarting tomcat5.5:     $(date)" >> /var/log/tomcat.txt
        /etc/init.d/tomcat5.5 start
fi




>>What was the problem with this version?

When I run the script from the prompt I get the "Usage" line returned.  And it just sits there.  I have to hit ctrl -c to break out.

[17:12:09] [root@server /sbin]# check_tomcat.sh

[17:12:19] [root@server /sbin]# Usage: /etc/init.d/tomcat5.5 {start|stop|restart|try-restart|force-reload|status}.
simon3270Commented:
It looks as though you are still running the version with the & - the bash prompt comes out before the "usage" text. Are you sure it hangs? If you just press Enter, I think it will give you another bash prompt.

I think you may be running a version of the script on your path. Try running

   ./check_tomcat.sh

to force it to run the version in the current directory.
mobotAuthor Commented:
Hey it's working fine when running as a cron job.  So that's good enough for me.  And I'm sure I don't have the "&" in the script.  And I'm familiar with ./  I've ran into that one before.  Anyway thanks for getting back to me.  I'll sort out why I have a problem running it from the cmd line later.  Appreciate your help.
simon3270Commented:
No problem. I only suggested the./ because root almost certainly doesn't (and shouldn't!) have "."  in its path. I once spent far too long wondering why a script output wouldn't change, whatever I did to it - it was only when I added "exit 0" as the first line that I worked out I was running a different script!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.