How to start a Java program as a Linux daemon with an /etc/init.d script.
See also:
- http://www.source-code.biz/snippets/java/7.htm
- JBoss Daemon Startup Script
- Waiting for a process to stop
Here is the CentOS/RedHat/Ubuntu version of the SUSE script:
10 Comments
Hide/Show CommentsAug 03, 2010
Anonymous
Nice! I stumbled upon the SUSE version myself. I liked it in the abstract, but got frustrated half-way through converting it to a Debian style init script. Thanks for the hard work!
Aug 04, 2010
Joshua Davis
You're welcome. I still have to make one that really works for tomcat. There are many floating around out there, but most leave you with zombie JVMs because they don't wait for the JVM to stop properly.
Sep 17, 2010
Anonymous
Hi Joshua,
Great script. It works flawlessly under CentOS 5.5.
Thought, I am unable to start up the script on boot. Ran "chkconfig --add service".
Anything that I missed?
Thanks
Sep 18, 2010
Joshua Davis
Hmm. I'll check that and add instructions.
Sep 19, 2010
Joshua Davis
Did you try using
after you added the service?
Sep 20, 2010
Anonymous
Hi Josua,
I actually ran:
# chkconfig --levels 345 myservice on
And check it via:
# chkconfig --list | grep myservice
myservice 0:off 1:off 2:off 3:on 4:on 5:on 6:off
I'll see if I can find anything within the script that you modified.
BTW - In the script, there are a few things:
1) Need extra # for first line. The HTML editor might have stripped it
2) The following two lines are no longer in used:scriptFile=$(readlink -fn $(type -p $0)) # the absolute, dereferenced path of this script file
scriptDir=$(dirname $scriptFile) # absolute path of the script directory
Sep 21, 2010
Joshua Davis
Okay, it's fixed now. Thanks!
Sep 20, 2010
Anonymous
Hi Joshua,
After checking the script, I've found the line that fails:
sudo -u $serviceUser -H $SHELL -c "$cmd" || return 1
I've updated the line to use "su" instead:
su -m $serviceUser -s $SHELL -c "$cmd" || return 1
Hope this helps others as well.
Thanks!
Sep 21, 2010
Joshua Davis
I see. Since the script is normally running as root anyway 'sudo' is a little redundant. I'll change it.
Mar 16, 2012
Anonymous
Hi Joshua,
First off thanks for a great script! It really helped out being a scripting newbie.
After going through the script I made a few updates:
I removed the --binary option from the 'grep' command in 'checkPrcoessIsOurService' since it only does something in MSDOS
Unsure if this would apply to others but the java program that I am running as a service already does it's own logging so I decided to only write ErrorStream to serviceLogFile (otherwise I was just duplicating the OutputStream)
Also unsure if this would apply to others, but I added the following line to "function stopService" after the first if statement to verify the user running the script will be able to remove the pid file before allowing them to stop the service. I ran into the situation where the serviceUser was able to kill the process but could not remove the pid file since the pid file was in /var/run.
if [ ! -w `dirname $pidFile` ]; then echo "Can not remove PID file - Permission Denied"; RETVAL=1; return 1; fi