Solved

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

Posted on 2009-07-03
19
1,484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +2
19 Comments
 
LVL 45

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 400 total points
ID: 24778374
try something like this:

#!/bin/bash

exitcode=1001
while [ exitcode -eq 1001 ]
do
    java ......
    exitcode=${?}
done
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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 45

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 45

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 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

617 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