Daemonizing a Process in Linux
Most of the times when we need to run some Java, Node.js or python program in background, so it could stay running even after you leave console all we do is put “&” in the end of the command.
$java -jar SimpleService.jar &
The problem here is that when you leave the bash shell your process will become “Orphan” and it’s up to the OS if INIT should adopt the process or kill the process.
To fix this we can force the process to be detached from your shell and become child process of INIT by putting nohup in front of command.
$nohup java -jar SimpleService.jar &
It’s all good for testing purposes, but services are supposed to run as “daemons” under some service supervisor. You can find many service supervisors with which you manage your services on Linux and we will cover few of the defaults that come with Linux.
Why do you need it ?
a. auto start/stop the service on startup/shutdown
b. controlling which service should start first
c. controlling which service should stop first is equally important
d. INIT controlled processes directly which provides more options like respawn, post-start, etc..
Ubuntu services using Upstart
Modify the script according to your setup:
$sudo vi /etc/init/java-app.conf
[js]author "nitin"
description "start and stop java-app for Ubuntu (upstart)"
version "1.0"
start on started networking
stop on runlevel [!2345]
# Automatically Respawn a process if fails, try max 5 times in 60 sec then give up
respawn
respawn limit 5 60
# Open files Limit
limit nofile 32768 32768
env APPUSER="admin"
env APPBIN="/usr/bin/java"
env APPARGS="-jar /opt/app/SimpleService.jar"
script
exec su – $APPUSER -c "$APPBIN $APPARGS"
end script
post-stop script
sleep 2
end script[/js]
Check the configuration:
$init-checkconf /etc/init/java-app.conf
Reload New configuration:
$sudo initctl reload-configuration
Check is service is known to Upstart:
$sudo initctl list |grep java
Start the service with:
$sudo start java-app
Note: For any errors look into /var/log/syslog and /var/log/upstart/*
Debian and Ubuntu services using SysVinit
Modify the script according to your setup.
$sudo vi /etc/init.d/java-app
[js]#!/bin/sh
### BEGIN INIT INFO
# Provides: daemon
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Java-app
# Description: Java-app start-stop-daemon – Debian
### END INIT INFO
NAME="java-app"
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
APPDIR="/opt/app"
APPBIN="/usr/bin/java"
APPARGS="-jar /opt/app/SimpleService.jar"
USER="admin"
GROUP="admin"
# Include functions
set -e
. /lib/lsb/init-functions
start() {
printf "Starting ‘$NAME’… "
start-stop-daemon –start –chuid "$USER:$GROUP" –background –make-pidfile –pidfile /var/run/$NAME.pid –chdir "$APPDIR" –exec "$APPBIN" — $APPARGS || true
printf "done\n"
}
#We need this function to ensure the whole process tree will be killed
killtree() {
local _pid=$1
local _sig=${2-TERM}
for _child in $(ps -o pid –no-headers –ppid ${_pid}); do
killtree ${_child} ${_sig}
done
kill -${_sig} ${_pid}
}
stop() {
printf "Stopping ‘$NAME’… "
[ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || \
while test -d /proc/$(cat /var/run/$NAME.pid); do
killtree $(cat /var/run/$NAME.pid) 15
sleep 0.5
done
[ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || rm /var/run/$NAME.pid
printf "done\n"
}
status() {
status_of_proc -p /var/run/$NAME.pid "" $NAME && exit 0 || exit $?
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $NAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0[/js]
Make sure the script is executable:
$chmod +x /etc/init.d/java-app
Enable the service to auto start in runlevel 2345:
$update-rc.d java-app defaults
Start the service with:
$service java-app start
CentOS/Redhat services using SysVinit
Modify the parameter according to your setup:
$sudo vi /etc/init.d/example
[js]#!/bin/sh
#
# example start stop daemon for CentOS (sysvinit)
#
# chkconfig: – 64 36
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 2 3 4 6
# Required-Start:
# description: java-app start stop daemon for CentOS
# processname: java-app
# pidfile: none
# lockfile: /var/lock/subsys/java-app
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
USER="admin"
APPNAME="java-app"
APPBIN="/usr/bin/java"
APPARGS="-jar /opt/app/SimpleService.jar"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"
LOGPATH=$(dirname $LOGFILE)
start() {
[ -x $prog ] || exit 5
[ -d $LOGPATH ] || mkdir $LOGPATH
[ -f $LOGFILE ] || touch $LOGFILE
echo -n $"Starting $APPNAME: "
daemon –user=$USER "$APPBIN $APPARGS >>$LOGFILE &"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n $"Stopping $APPNAME: "
killproc $APPBIN
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
return $RETVAL
}
restart() {
stop
start
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
status)
rh_status
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac[/js]
Make sure the script is marked as executable:
$sudo chmod +x /etc/init.d/java-app
Enable the service to auto start at runlevels 2345:
$sudo chkconfig java-app on
Start the service with:
$sudo service java-app start
This is how simple it is to daemonize a process. In my next blog, I will talk about more interesting features available in Linux OS.