Red Hat init-script Vorlagen

Sobald man mal ein eigenes start script schreiben will, sucht man nach einem guten template, welches Möglichst die Standard Funktionen benutzt; diese sind bei RedHat z.B. in der Datei: /etc/init.d/functions zu finden.

Nachfolgend finden sich zwei Beispiele; das erste benutzt man, wenn man das „daemon-handling“ selbst machen will, dass zweite nutzt die entsprechende Funktion in der functions Datei.

# Init-Script template
#
# Written by Steven Varco <email@adresse.tld>
#
# chkconfig: - 80 20
# description: SERVICENAME daemon
#
# processname: SERVICENAME
# config: /etc/SERVICENAME.conf
# pidfile: /var/run/SERVICENAME
 
# source functions library
. /etc/init.d/functions
 
 
APP_NAME=myApp                                  # Name of the application (will be excecuted!)
APP_DESC="SERVICE DESCRIPTION"                  # Short description
APP_PID=/var/run/$APP_NAME                      # PID file
APP_CONFIG="/etc/SERVICENAME.conf"              # Config file
 
 
case "$1" in
start)
  echo -n "Starting $APP_NAME..."
 
  # Start functions
  #$(which runuser) -l myuser $APP_NAME
  echo $! > $APP_PID
 
  # Output OK or FAIL
  RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    echo_success
  else
    echo_failure
  fi
 
  # Create PID-File
  if [ ! -f "$APP_PID" ] ; then
    touch $APP_PID
  fi
;;
 
 
 
stop)
  echo "Stopping $APP_NAME..."
 
# Stop functions
  if [ -f $APP_PID ] ; then
    kill $($APP_PID)
 
    # Output OK or FAIL
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      echo_success
    else
      echo_failure
    fi
  else
    echo "$APP_NAME is not running"
  fi
 
  # Remove PID-File
  if [ -f "$APP_PID" ] ; then
    rm -f $APP_PID
  fi
;;
 
 
 
restart)
$0 stop && sleep 3 && $0 start || return=$0
;;
 
 
status)
  if [ -f "$APP_PID" ] ; then
    PID=$(cat $PID_FILE)
    PSPID=$(ps -p $PID -o comm=)
    if [ -n "$PSPID" ]; then
      echo -n "$APP_NAME is running as PID: $PID and command: $PSPID"
      echo_success
    else
      echo -n "$APP_NAME dead but pid file exists!"
      echo_warn
    fi
  else
    echo -n "$APP_NAME is not running"
  fi
  exit $RETVAL
;;
 
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
esac
#!/bin/bash
#
# Init file for myApp
#
# Written by Vorname Name <email@adresse.tld>.
#
# chkconfig: - 80 20
# description: MyApp daemon
#
# processname: myapp
# config: /etc/myapp.cfg
# pidfile: /var/run/myapp
 
source /etc/rc.d/init.d/functions
 
### Default variables
CONFIG="/etc/myapp.cfg"
 
[ -x /usr/sbin/myapp ] || exit 1
[ -r "$CONFIG" ] || exit 1
 
RETVAL=0
prog="myapp"
desc="myApp daemon"
 
start() {
        echo -n $"Starting $desc ($prog): "
        daemon $prog -c "$CONFIG" -d
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
 
stop() {
        echo -n $"Shutting down $desc ($prog): "
        killproc $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        return $RETVAL
}
 
restart() {
        stop
        start
}
 
reload() {
        echo -n $"Reloading $desc ($prog): "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
*)
  echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
  RETVAL=1
esac
 
exit $RETVAL

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert