Initscripts/Writing rc.d scripts

From ArchWiki
Revision as of 03:04, 14 January 2011 by Lavandero (Talk | contribs) (gaps and markup style)

Jump to: navigation, search
Summary help replacing me
How to write rc.d (daemon) scripts.
Related
Daemon
rc.conf
Arch_Boot_Process

Introduction

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.

Basic scheme

Most rc.d scripts follow the same structure but the details are typically specific to the daemon.

Daemon arguments

Arguments to your daemon should be placed in Template:Filename

Prototype

#!/bin/bash

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

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

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

case "$1" in
  start)
    stat_busy "Starting $DAEMON daemon"
    [ -z "$PID" ] && $DAEMON &>/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

Tip: Template:Filename and Template:Filename contain many examples

A proper example for tftpd:

Template:File

Template:File

Tips