Hi,
How would get the application termination code e.g., 1001 ans pass it to the application ??
Main Topics
Browse All TopicsHello,
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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
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?
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
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/
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?
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/
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
Business Accounts
Answer for Membership
by: KdoPosted on 2009-07-04 at 16:27:48ID: 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