Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-03
19
Medium Priority
?
1,503 Views
Last Modified: 2013-12-06
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
0
Comment
Question by:richardsimnett
  • 6
  • 5
  • 4
  • +2
17 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24778358
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24778359
Hi,

How would get the application termination code e.g., 1001 ans pass it to the application ??
0
 
LVL 92

Accepted Solution

by:
objects earned 1600 total points
ID: 24778374
try something like this:

#!/bin/bash

exitcode=1001
while [ exitcode -eq 1001 ]
do
    java ......
    exitcode=${?}
done
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Duncan Roe
ID: 24778492
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
 

Author Comment

by:richardsimnett
ID: 24782532
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
 
LVL 92

Expert Comment

by:objects
ID: 24782551
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
 

Author Comment

by:richardsimnett
ID: 24782651
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
 
LVL 92

Expert Comment

by:objects
ID: 24782678
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24784442
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
 

Author Comment

by:richardsimnett
ID: 24788349
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24788419
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
 

Author Comment

by:richardsimnett
ID: 24788575
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
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 400 total points
ID: 24788620
Oops.  :)

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


Kent
0
 

Author Comment

by:richardsimnett
ID: 24788801
kdo,
I dont understand.
0
 

Author Comment

by:richardsimnett
ID: 24789319
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
 
LVL 92

Expert Comment

by:objects
ID: 24790039
>     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
 
LVL 92

Expert Comment

by:objects
ID: 24790052
thats why I suggested you need a separate script for the init script as the init script needs to background the process
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question