Initscripts/Writing rc.d scripts

From ArchWiki
Revision as of 03:18, 14 January 2011 by Lavandero (Talk | contribs) (decrease nesting)

Jump to: navigation, search

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

As part of Arch's 'BSD-style' init, rc.d scripts are used to control the starting,stopping and restarting of daemons. This guide will help to create your own rc.d scripts.

Guideline

Prototype

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

DAEMON=DAEMON_NAME
ARGS=

[ -r /etc/conf.d/$DAEMON.conf ] && . /etc/conf.d/$DAEMON.conf

PID=$(pidof -o %PPID $DAEMON)

case "$1" in
  start)
    stat_busy "Starting $DAEMON daemon"
    [ -z "$PID" ] && $DAEMON $ARGS &>/dev/null
    if [ $? = 0 ]; then
      add_daemon $DAEMON
      stat_done
    else
      stat_fail
      exit 1
    fi
    ;;
  stop)
    stat_busy "Stopping $DAEMON daemon"
    [ -n "$PID" ] && kill $PID &>/dev/null
    if [ $? = 0 ]; then
      rm_daemon $DAEMON
      stat_done
    else
      stat_fail
      exit 1
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    stat_busy "Checking $DAEMON status"
    ck_status $DAEMON
    ;;
  *)
    echo "usage: $0 {start|stop|restart|status}"
esac

Example

The following is an example for tftpd. Look in Template:Filename for a greater variety.

The configuration file: Template:File

The actual script: Template:File