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. [↩]
Thanks! That was really useful!
Keep up the good work!
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!
Thanks Kus!
script very good, nice job.
= )
Thanks for sharing this script Wes!
Thanks for the update Kus!
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
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…
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
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
Is there a reason this script doesn’t conform to the LSB? (Linux Standards Base)
http://wiki.debian.org/LSBInitScripts
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!
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
Pingback: » Python:How to make a python script run like a service or daemon in linux
Pingback: Unix/Linux:adding a program on start-up by using LSBInitScripts – Unix Questions