Solved

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

Posted on 2009-07-03
19
1,456 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
19 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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 400 total points
Comment Utility
try something like this:

#!/bin/bash

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

Expert Comment

by:Duncan Roe
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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 45

Assisted Solution

by:Kdo
Kdo earned 100 total points
Comment Utility
Oops.  :)

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


Kent
0
 

Author Comment

by:richardsimnett
Comment Utility
kdo,
I dont understand.
0
 

Author Comment

by:richardsimnett
Comment Utility
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
Comment Utility
>     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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now