Skip to content
Steve Cote edited this page Apr 19, 2017 · 3 revisions

This is a template for loading your Java program as a background process:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          coyote
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop coyote server
### END INIT INFO
 
case $1 in
    start)
        echo "Starting Coyote ..."
        if [ ! -f /usr/local/coyote/pid ]; then
            nohup java -jar /usr/local/coyote/coyote_server.jar /usr/local/coyote 2>> /dev/null >> /dev/null &
            echo $! > /usr/local/coyote/pid
            echo "Coyote started ..."
        else
            echo "Coyote is already running ..."
        fi
    ;;
    stop)
        if [ -f /usr/local/coyote/pid ]; then
            PID=$(cat /usr/local/coyote/pid);
            echo "Stopping Coyote ..."
            kill $PID;
            echo "coyote stopped ..."
            rm /usr/local/coyote/pid
        else
            echo "Coyote is not running ..."
        fi
    ;;
    restart)
        if [ -f /usr/local/coyote/pid ]; then
            PID=$(cat /usr/local/coyote/pid);
            echo "Stopping Coyote ...";
            kill $PID;
            echo "Coyote stopped ...";
            rm /usr/local/coyote/pid
 
            echo "Starting Coyote ..."
            nohup java -jar /usr/local/coyote/coyote_server.jar /usr/local/coyote 2>> /dev/null >> /dev/null &
            echo $! > /usr/local/coyote/pid
            echo "Coyote started ..."
        else
            echo "coyote is not running ..."
        fi
    ;;
esac

To use it for your own purposes do the following:

  • replace the string coyote in the script with your program name,
  • replace /usr/local/coyote/pid with the folder where you’ll be running your program,
  • replace “nohup java -jar /usr/local/coyote/coyote_server.jar” with the path to your own program

Now save the file to “yourprogramname.sh” (for my purpose coyote) and copy it to the daemon folder (run the code below).

sudo mv coyote.sh /etc/init.d/coyote
sudo chmod +x /etc/init.d/coyote
sudo update-rc.d coyote defaults

Your Java daemon is now set to start everytime your turn your PC on. You can easily stop it by typing this in the terminal (replace coyote with your own program):

sudo /etc/init.d/coyote stop

You should get a message that Coyote is not running ....

Linux Standard Base (LSB)

The above script is almost a LSB-compliant init script; it contains a dependency header but does not contains a force-reload action.

Refer to installing init.d scripts with LSB for more information on installing the above script with LSB.

Clone this wiki locally