Init.d servizi di sistema autostart

Ecco un'esempio di script da usare nella cartella di #etc/init.d

01 #!/bin/bash
02 # myapp daemon
03 # chkconfig: 345 20 80
04 # description: myapp daemon
05 # processname: myapp
06
07 DAEMON_PATH="/home/wes/Development/projects/myapp"
08
09 DAEMON=myapp
10 DAEMONOPTS="-my opts"
11
12 NAME=myapp
13 DESC="My daemon description"
14 PIDFILE=/var/run/$NAME.pid
15 SCRIPTNAME=/etc/init.d/$NAME
16
17 case "$1" in
18 start)
19 printf "%-50s" "Starting $NAME..."
20 cd $DAEMON_PATH
21 PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
22 #echo "Saving PID" $PID " to " $PIDFILE
23 if [ -z $PID ]; then
24 printf "%s\n" "Fail"
25 else
26 echo $PID > $PIDFILE
27 printf "%s\n" "Ok"
28 fi
29 ;;
30 status)
31 printf "%-50s" "Checking $NAME..."
32 if [ -f $PIDFILE ]; then
33 PID=`cat $PIDFILE`
34 if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
35 printf "%s\n" "Process dead but pidfile exists"
36 else
37 echo "Running"
38 fi
39 else
40 printf "%s\n" "Service not running"
41 fi
42 ;;
43 stop)
44 printf "%-50s" "Stopping $NAME"
45 PID=`cat $PIDFILE`
46 cd $DAEMON_PATH
47 if [ -f $PIDFILE ]; then
48 kill -HUP $PID
49 printf "%s\n" "Ok"
50 rm -f $PIDFILE
51 else
52 printf "%s\n" "pidfile not found"
53 fi
54 ;;
55
56 restart)
57 $0 stop
58 $0 start
59 ;;
60
61 *)
62 echo "Usage: $0 {status|start|stop|restart}"
63 exit 1
64 esac

Una volta creato il proprio script personale è indispensabile  specificare il runlevel tramite il comando #update-rc.d nomeservizio defaults

PS.

E' necessario specificare le direttive a inizio file altrimenti potrebbe dare errori nel caricamento automatico come servizio:

### BEGIN INIT INFO
# Provides:          example
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO
Italian - ItalyEnglish (United Kingdom)