Difference between revisions of "Initscripts/Writing rc.d scripts"

From ArchWiki
Jump to: navigation, search
m (gaps and markup style)
(Tips: most daemons create their own pid file: exception, not the rule; grammar; random bullet nesting)
Line 115: Line 115:
  
 
==Tips==
 
==Tips==
*Follow the same structure as other rc.d script
+
*Follow the same structure as other rc.d scripts
**Source {{Filename|/etc/rc.conf}}, {{Filename|/etc/rc.d/functions}}, and optionally {{Filename|/etc/conf.d/DAEMON_NAME}}
+
*Source {{Filename|/etc/rc.conf}}, {{Filename|/etc/rc.d/functions}}, and optionally {{Filename|/etc/conf.d/DAEMON_NAME}}
***Use functions in /etc/rc.d/functions
+
*Use functions in /etc/rc.d/functions
*Create /var/run/DAEMON_NAME.pid
+
*Include at least start, stop and restart
*Include at least start, stop and restart.
+

Revision as of 03:06, 14 January 2011

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

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