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

From ArchWiki
Jump to: navigation, search
(Prototype: in practice, the abs example is ignored, for good reasons: redundant forking to check pid; stick to existing conventions)
m (gaps and markup style)
Line 1: Line 1:
 
[[Category:Boot process (English)]]
 
[[Category:Boot process (English)]]
 
[[Category:HOWTOs (English)]]
 
[[Category:HOWTOs (English)]]
 
 
{{Article summary start}}
 
{{Article summary start}}
 
{{Article summary text|How to write rc.d (daemon) scripts.}}
 
{{Article summary text|How to write rc.d (daemon) scripts.}}
Line 69: Line 68:
  
 
==Example==
 
==Example==
 
 
{{Tip|{{Filename|/etc/rc.d/}} and {{Filename|/etc/conf.d/}} contain many examples}}
 
{{Tip|{{Filename|/etc/rc.d/}} and {{Filename|/etc/conf.d/}} contain many examples}}
 
  
 
A proper example for tftpd:
 
A proper example for tftpd:
Line 77: Line 74:
 
{{File|name=/etc/conf.d/tftpd|content=<nowiki>TFTPD_ARGS="-l -s /var/tftpboot -c"</nowiki>}}
 
{{File|name=/etc/conf.d/tftpd|content=<nowiki>TFTPD_ARGS="-l -s /var/tftpboot -c"</nowiki>}}
  
 
+
{{File|name=/etc/rc.d/tftpd|content=<nowiki>
{{File|name=/etc/rc.d/tftpd|content=<nowiki> #!/bin/bash
+
#!/bin/bash
 
. /etc/rc.conf
 
. /etc/rc.conf
 
. /etc/rc.d/functions
 
. /etc/rc.d/functions
 +
 
TFTPD_ARGS=
 
TFTPD_ARGS=
 
[ -f /etc/conf.d/tftpd ] && . /etc/conf.d/tftpd
 
[ -f /etc/conf.d/tftpd ] && . /etc/conf.d/tftpd
 
PID=`pidof -o %PPID /usr/sbin/in.tftpd`
 
PID=`pidof -o %PPID /usr/sbin/in.tftpd`
 +
 
case "$1" in
 
case "$1" in
 
  start)
 
  start)
Line 97: Line 96:
 
  stop)
 
  stop)
 
   stat_busy "Stopping TFTPD"
 
   stat_busy "Stopping TFTPD"
   [ ! -z "$PID" ]  && kill $PID &> /dev/null
+
   [ -n "$PID" ]  && kill $PID &> /dev/null
 
   if [ $? -gt 0 ]; then
 
   if [ $? -gt 0 ]; then
 
     stat_fail
 
     stat_fail
Line 120: Line 119:
 
***Use functions in /etc/rc.d/functions
 
***Use functions in /etc/rc.d/functions
 
*Create /var/run/DAEMON_NAME.pid
 
*Create /var/run/DAEMON_NAME.pid
*Include at least start,stop and restart.
+
*Include at least start, stop and restart.

Revision as of 03:04, 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