Init Script on Linux
From Tuxisalive
This article describes how to write and install an initialization script for the tuxhttpserver on Linux.
Contents |
Why using an init script for the server
The APIs control Tux Droid robot through requests to a HTTP server: this one must be launched to make Tux Droid running. Creating an init script allows to automatically start the server at different runlevels.
What is an init script and how to install it
Basics
Basically, and initialization script is a simple bash script automating a program launch. This script usually has three options :
- start
- stop
- restart
More options can be found, like reload, force-reload, force-restart, etc In the case of the Tux Droid HTTP server the program to launch is a daemon.
General structure
The general structure is :
#!/bin/bash case "$1" in start) # Code to start the daemon ;; stop) # Code to stop the daemon ;; restart) $0 stop $0 start ;; *) echo "Usage : start | stop | reload" ;; esac
The way to start or stop the daemon is dependent of the distribution. For more information, read your distribution documentation. Most of distribution have a skeleton for the initialization script. It's usually a good base to start.
I've written my init script. And now ?
Once your script is written, put it in the right directory. This directory is also depending of your distros. Usually, the directory is /etc/init.d, but on some distros, it's /etc/rc.d .
You can test your script easily :
# /etc/init.d/myscript start
or
# /etc/rc.d/myscript start
How to install the initialization script
Putting the script on /etc/init.d or /etc/rc.dis not sufficient to launch it at startup. You have to configure your system to allows it's execution at startup, and to stop it when your computer shutdowns.
Most of distribution have a specific tool to install it automatically :
- update-rc.d for debian based distros :
update-rc.d "myscript" defaults will install it at default runlevels. See the man page of update-rc.d to know how to install your script at specifics runlevels.
- chkconfig for for redhat based distros :
chkconfig --add "myscript" will install it at defaults runlevels. See the man page of chkconfig to know how to install your script at specifics runlevels.
- ...
Examples
If you have written an init script for your distribution, you can add the code and explain how to install it here.
Arch Linux
#!/bin/bash PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="HTTP server for Tuxdroid" NAME=tuxhttpserver DAEMON=/opt/tuxdroid/tuxhttpserver/tuxhttpserver.py DAEMON_ARGS="" PIDFILE=/var/run/$NAME.pid . /etc/rc.conf . /etc/rc.d/functions case "$1" in start) stat_busy "Starting tuxhttpserver" python $DAEMON if [ $? -gt 0 ]; then stat_fail else add_daemon $NAME stat_done fi ;; stop) stat_busy "Stopping tuxhttpserver" if [ -a $PIDFILE ] then PID=`cat $PIDFILE` kill $PID > /dev/null if [ $? -gt 0 ]; then stat_fail else rm $PIDFILE rm_daemon $NAME stat_done fi else stat_done fi ;; restart|force-reload) $0 stop sleep 1 $0 start ;; esac exit 0
This script must be placed on /etc/rc.d/tuxhttpserver, and must be executable :
chmod +x /etc/rc.d/tuxhttpserver
To install it, edit /etc/rc.conf, and add if to the DEAMON list :
DAEMONS=(... tuxhttpserver ...)
And start it :
/etc/rc.d/tuxhttpserver start
Gentoo
#!/sbin/runscript # Copyright 1999-2009 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header: $ depend() { need alsasound } start() { ebegin "Starting Tux HTTP Server" start-stop-daemon --start -p /var/run/tuxhttpserver.pid --exec /usr/share/tuxdroid/tuxhttpserver/tuxhttpserver.py --name python eend $? } stop() { ebegin "Stopping Tux HTTP Server" start-stop-daemon --stop --quiet -p /var/run/tuxhttpserver.pid eend $? } restart() { ebegin "Nothing to do :/" /bin/false eend $? }
This script must be placed on /etc/init.d/tuxhttpserver, and must be executable:
chmod +x /etc/init.d/tuxhttpserver
To install it, use rc-update command:
rc-update add tuxhttpserver default
And start it:
/etc/init.d/tuxhttpserver start
Mandriva
#!/bin/sh # chkconfig: 345 56 50 # description: This startup script launches Tuxdroid HTTP Server ### BEGIN INIT INFO # Provides: tuxhttpserver # Required-Start: # Required-Stop: # Default-Start: 345 # Short-Description: Tuxdroid HTTP Server # Description: This startup script launches Tuxdroid HTTP Server ### END INIT INFO PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="HTTP server for Tuxdroid" NAME=tuxhttpserver PROCESS=$NAME DAEMON="python /opt/tuxdroid/tuxhttpserver/tuxhttpserver.py" DAEMON_ARGS="" PIDFILE=/var/run/$NAME.pid # Source function library. . /etc/init.d/functions case "$1" in start) if [ -f $PIDFILE ] ; then gprintf "$NAME is already started\n" exit 3 fi gprintf "Starting %s" "$NAME" daemon $DAEMON echo ;; stop) if ! [ -f $PIDFILE ] ; then gprintf "$NAME is already stopped\n" exit 3 fi gprintf "Stopping %s" "$NAME" killproc -p $PIDFILE $PROCESS echo exit 3 ;; restart|force-reload) $0 stop sleep 1 $0 start ;; esac exit 0
This script must be placed on /etc/init.d/tuxhttpserver, and must be executable :
chmod +x /etc/init.d/tuxhttpserver
To install it :
chkconfig --add tuxhttpserver
And to start it :
/etc/init.d/tuxhttpserver start
Ubuntu
#! /bin/sh ### BEGIN INIT INFO # Provides: tuxhttpserver # Required-Start: $local_fs $remote_fs # Required-Stop: $local_fs $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: S 0 1 6 # Short-Description: Start stop Tuxdroid HTTP REST server. # Description: Start stop Tuxdroid HTTP REST server. ### END INIT INFO PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="HTTP server for Tuxdroid" NAME=tuxhttpserver DAEMON=/usr/bin/$NAME DAEMON_ARGS="" PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME # Exit if the package is not installed [ -x "$DAEMON" ] || exit 0 # Load the VERBOSE setting and other rcS variables [ -f /etc/default/rcS ] && . /etc/default/rcS # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions case "$1" in start) test -f $PIDFILE && exit 0 log_daemon_msg "Starting $DESC" "$NAME" start-stop-daemon --start --quiet --background --exec $DAEMON -- $DAEMON_ARGS log_end_msg 0 ;; stop) test -f $PIDFILE || exit 0 log_daemon_msg "Stopping $DESC" "$NAME" start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null rm -f $PIDFILE log_end_msg 0 ;; restart|force-reload) log_daemon_msg "Restarting $DESC" "$NAME" $0 stop sleep 1 $0 start ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 exit 3 ;; esac
This script must be placed on /etc/init.d/tuxhttpserver, and must be executable :
chmod +x /etc/init.d/tuxhttpserver
To install it :
update-rc.d tuxhttpserver defaults
And start it :
/etc/init.d/tuxhttpserver start
Fedora
#!/bin/sh # chkconfig: 345 56 50 # description: This startup script launches Tuxdroid HTTP Server ### BEGIN INIT INFO # Provides: tuxhttpserver # Required-Start: # Required-Stop: # Default-Start: 345 # Short-Description: Tuxdroid HTTP Server # Description: This startup script launches Tuxdroid HTTP Server ### END INIT INFO PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="HTTP server for Tuxdroid" NAME=tuxhttpserver PROCESS=$NAME DAEMON="python /usr/share/tuxdroid/tuxhttpserver/tuxhttpserver.py" DAEMON_ARGS="" PIDFILE=/var/run/$NAME.pid # Source function library. . /etc/init.d/functions case "$1" in start) if [ -f $PIDFILE ] ; then echo "$NAME is already started" exit 3 fi printf "Starting $NAME" daemon $DAEMON echo ;; stop) if ! [ -f $PIDFILE ] ; then echo "$NAME is already stopped" exit 3 fi printf "Stopping $NAME" killproc -p $PIDFILE $PROCESS echo exit 3 ;; restart|force-reload) $0 stop sleep 1 $0 start ;; esac exit 0
Create a new empty file called 'tuxhttpserver', and copy the above script in it, and save the file.
Once the script is done, you can install it on your system :
chmod +x tuxhttpserver su cp tuxhttpserver /etc/init.d chkconfig --add tuxhttpserver exit

