How would I restart a program based on its exit code?

Hello,
I have a startup script which starts a java applicaton and runs it as a daemon in centos. I need to have the script somehow restart the java application if it quits with a specific error code (1001). How owuld I do this?

Example bash is a plus.

Thanks,
Rick
richardsimnettAsked:
Who is Participating?
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.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Rick,

Write a small shell script to run the application and have the script run at startup instead of the application.  The script can test the exit code and restart the application based on almost and criteria that you choose.


Good Luck,
Kent
0
Kerem ERSOYPresidentCommented:
Hi,

How would get the application termination code e.g., 1001 ans pass it to the application ??
0
objectsCommented:
try something like this:

#!/bin/bash

exitcode=1001
while [ exitcode -eq 1001 ]
do
    java ......
    exitcode=${?}
done
0

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Duncan RoeSoftware DeveloperCommented:
You need to choose an exit code lower than 128. If in your java app you have coded "exit 1001" or however you do it in java, your actual exit code will be 233 - you could substitute for 1001 in the bash example above and try that.
The reason for this is that the underlying exit function that java eventually gets to will mask its argument to keep only the least significant 8 bits. Exit codes are 16-bit entities where values of 128 and higher indicate that the program terminated with a signal. For example exit code 143 means the program was terminated - signal SIGTERM has value 15: 128 + 15 = 143.
You might find it less confusing to explicitly code a lower value in your java app, but that isn't actually necessary
0
richardsimnettAuthor Commented:
Ok guys Im really confused. Please see my script below, I thought after the java was executed the startup script terminated, so im not really sure how you would capture the exit code.

What will happen is this:

The app is started and running.

At any time while the application is runni9ng, a restart command can be issued to the app via a web based interface.

When the command is received, the application shuts itself down with a specific exit code.

Somehow the java application is supposed to be restarted on the OS side, via its startup script or other means.

Thanks,
Rick
#!/bin/bash
#
# Mail Server       This shell script starts and stops the Mail Server
#
# Author:       Some Dude
#
# chkconfig:    - 50 01
#
# description:  Mail Server
# processname:  Mail Server
#
#
 
# source function library
. /etc/rc.d/init.d/functions
 
lockfile=/var/lock/subsys/MailServer
PID=0
RETVAL=0
 
start() {
       echo -n $"Starting Mail Server: "
       if [ -f $lockfile ]; then
           echo -n $"Mail Server is already running." && failure
           RETVAL=3
       else
	   cd /opt/mps
           touch "$lockfile" && success || failure
           java -Xmx128M -jar mail_server.jar > /var/log/mail_server.log & echo $! > $lockfile
 
           RETVAL=$?
       fi
       echo
}
 
stop() {
       echo -n $"Stopping Mail Server: "
       if [ -f $lockfile ]; then
          PID=`cat /var/lock/subsys/MailServer`
          kill -9 $PID
          rm -f "$lockfile" && success
          RETVAL=0
       else
          echo -n $"Mail Server is not started." && failure
          RETVAL=3
       fi
       echo
}
 
restart() {
       stop
       start
}
 
case "$1" in
 start)
       start
       ;;
 stop)
       stop
       ;;
 restart|force-reload)
       restart
       ;;
 reload)
       ;;
 condrestart)
       [ -f "$lockfile" ] && restart
       ;;
 status)
       if [ -f $lockfile ]; then
               echo $"Mail Server is Currently Running."
               RETVAL=0
       else
               echo $"Mail Server is NOT Running."
               RETVAL=3
       fi
       ;;
 *)
       echo $"Usage: $0
{start|stop|status|restart|reload|force-reload|condrestart}"
       exit 1
esac
 
exit $RETVAL

Open in new window

0
objectsCommented:
don't implement it in your init script. Have your init script call another script (this script would be run in background) which handles the running/restarting of your app.
0
richardsimnettAuthor Commented:
objects,

Should the new script also be like an init script? Ie, start, stop, etc? Also, should this script also have its own PID file?

Im not 100% clear on this.

What I've pieced together so far, is have the init script run another script to start the java app and run it in the background, and have that script sit in a while loop, checking the exit code of the app and reacting accordingly. Is that correct?


0
objectsCommented:
no I'm thinking the new script will just handle the (re)starting the app
the pid file to use is managed by the init script so sounds like you should be passing that to the script that starts your app so it can store the pid in the correct file'
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi Richard,

In the script's start() function, the service is started by invoking the java interpreter ("java -Xmx128M ...").

At that point, invoke another script.  (In fact, you can invoke it asyncronously.  No point in leaving the initiation task in a wait state.)

Within the new script, run the java statement.

#!/bin/ksh
#
set STATUS=0
while [ $STATUS != 1001 ]
do
  java -Xmx128M -jar mail_server.jar > /var/log/mail_server.log & echo $! > $lockfile
  STATUS=$?
done
exit $STATUS

You might pass the name of the lockfile, handle the lockfile management here, etc.  But this is the general approach.


Good Luck,
Kent
0
richardsimnettAuthor Commented:
Hey guys,

Ok I have run into a bit of difficulty i tseems. heres what I have.

I created a startup.sh file, and have it being started from the init script.
Here is the startup.sh

#!/bin/bash

lockfile=/var/lock/subsys/mail_server
exitcode=101
while (( exitcode == 101 ))
do
    java -Xmx128M -jar mail_server.jar > /var/log/mail_server.log & echo $! > $lockfile
    exitcode=$?
done

The program starts and stops perfectly fine, setup liek this so far.

But when I issue the stop command, and the program quits with exit code 101, it does not restart. Any suggestions?

0
Kent OlsenData Warehouse Architect / DBACommented:
Hi Rick,

Do you know what exit code is being returned?  Perhaps echoing the value to a file will help.

#!/bin/bash

lockfile=/var/lock/subsys/mail_server
exitcode=101
while (( exitcode == 101 ))
do
    java -Xmx128M -jar mail_server.jar > /var/log/mail_server.log & echo $! > $lockfile
    exitcode=$?
    echo " Mail exited with code $exitcode " >> MailServer.log
done
0
richardsimnettAuthor Commented:
kdo,
ok I did what you suggested, and its not blocking while its waiting for the exitcode.

In other words, immediately upon the startup.sh being run and java is being created, im getting the mailserver.log file generated, with an exit code of 0.

Thanks,
Rick
0
Kent OlsenData Warehouse Architect / DBACommented:
Oops.  :)

Run the Java statement synchronously for this test.  :) :) :)


Kent
0
richardsimnettAuthor Commented:
kdo,
I dont understand.
0
richardsimnettAuthor Commented:
I got it. Take out the &.  and execute the startup.sh from the init with the &.

I did that and it seems to work, however this raises another problem. I cannot simply "stop" the daemon now (I think the pid is wrong now). Should I post this question as a new one?

Thanks,
Rick
0
objectsCommented:
>     java -Xmx128M -jar mail_server.jar > /var/log/mail_server.log & echo $! > $lockfile

you don't want to start it in the background there
0
objectsCommented:
thats why I suggested you need a separate script for the init script as the init script needs to background the process
0
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
Linux Distributions

From novice to tech pro — start learning today.