Recently I found the need to create an init.d script and since I had a hard time finding an example elsewhere1, here’s the overly simple script I came up with to get the job done:
#!/bin/bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp
DAEMON_PATH="/home/wes/Development/projects/myapp"
DAEMON=myapp
DAEMONOPTS="-my opts"
NAME=myapp
DESC="My daemon description"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s\n" "Fail"
else
echo $PID > $PIDFILE
printf "%s\n" "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -HUP $PID
printf "%s\n" "Ok"
rm -f $PIDFILE
else
printf "%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
This script will work in /etc/init.d on Xubuntu 11.10 (so most Debian-based systems) and CentOS 5.5 and you can control it via chkconfig.
- That said, if you know of such an example I’d love to hear from you. [↩]


#1 by Fernando on March 23, 2012 - 11:37 pm
Quote
Thanks! That was really useful!
Keep up the good work!
#2 by Kus on April 19, 2012 - 9:55 am
Quote
Hi!
I’ve just found your post which is overall useful for a quick script, but I’ll make a couple of corrections to it:
- line 34: s/5003/${PID}/
- line 50: s/rf/f/
Cheers!
#3 by wes on April 19, 2012 - 12:58 pm
Quote
Thanks Kus!
#4 by Robson on April 23, 2012 - 7:04 pm
Quote
script very good, nice job.
= )
#5 by Okezie on June 1, 2012 - 4:02 pm
Quote
Thanks for sharing this script Wes!
Thanks for the update Kus!
#6 by Okezie on June 17, 2012 - 8:45 pm
Quote
Service status error: line 34: [: too many arguments
Fixed by adding quotes around the ps... command.
Line 34: if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]
Pingback: run nodemon on startup on a raspberry pi | TheFabric.com
#7 by Ali Raza on February 1, 2013 - 3:52 am
Quote
Great work …..
I have a question… where I want to define the script name for which I am trying to make service … it may be a stupid question but I am confuse…
#8 by BackTrack on February 20, 2013 - 8:41 am
Quote
Thanks, very useful example.
Small correction:
If you try to execute command while service is stopped, if will return an error: cat: /var/run/app.pid: No such file or directory
This can be fixed by moving line 45 after line 47
#9 by [email protected] on March 15, 2013 - 11:08 am
Quote
Thanks you for the script, it’s very useful.
One question:
How can I make my init.d script starts just before syslog and stops just after it?
is that possible?
I’m running Centos5.5
#10 by Techplex Engineer on April 19, 2013 - 7:10 am
Quote
Is there a reason this script doesn’t conform to the LSB? (Linux Standards Base)
http://wiki.debian.org/LSBInitScripts
#11 by wes on April 19, 2013 - 7:34 am
Quote
I’m not opposed to making it conform to standards but not at the expense of the primary goal which is simplicity. If you know of an init.d script that conforms to the standards and is simple to use then I’d love to hear about it!
#12 by pdeschen on May 12, 2013 - 1:14 pm
Quote
Nice one. Check this one of mine taking into account sudo user, priority, and sysconfig.
https://gist.github.com/pdeschen/5564411
Cheers!
Pingback: Raspberry Pi Setup Notes » funkboxing