Skip to end of metadata
Go to start of metadata

How to start a Java program as a Linux daemon with an /etc/init.d script.

See also:

Here is the CentOS/RedHat/Ubuntu version of the SUSE script:

  1. Aug 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!

    1. Aug 04, 2010

      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.

  2. 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

    1. Sep 18, 2010

      Hmm. I'll check that and add instructions.

    2. Sep 19, 2010

      Did you try using

      after you added the service?

      1. 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

        1. Sep 21, 2010

          Okay, it's fixed now. Thanks!

      2. 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!

        1. Sep 21, 2010

          I see. Since the script is normally running as root anyway 'sudo' is a little redundant. I'll change it.

  3. 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