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

From ArchWiki
Jump to: navigation, search
(Tips: most daemons create their own pid file: exception, not the rule; grammar; random bullet nesting)
(consistency; merge section)
Line 2: Line 2:
 
[[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|Writing rc.d (daemon) scripts.}}
 
{{Article summary heading|Related}}
 
{{Article summary heading|Related}}
 +
{{Article summary wiki|Arch Boot Process}}
 
{{Article summary wiki|Daemon}}
 
{{Article summary wiki|Daemon}}
 
{{Article summary wiki|rc.conf}}
 
{{Article summary wiki|rc.conf}}
{{Article summary wiki|Arch_Boot_Process|Arch Boot Process}}
 
 
{{Article summary end}}
 
{{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 [[Daemon|daemons]]. This guide will help to create your own rc.d scripts.
 
As part of Arch's 'BSD-style' init, rc.d scripts are used to control the starting,stopping and restarting of [[Daemon|daemons]]. This guide will help to create your own rc.d scripts.
  
==Basic scheme==
+
==Guideline==
Most rc.d scripts follow the same structure but the details are typically specific to the daemon.
+
*Source {{Filename|/etc/rc.conf}}, {{Filename|/etc/rc.d/functions}}, and optionally {{Filename|/etc/conf.d/DAEMON_NAME}}.
 
+
*Arguments and other daemon options should be placed in {{Filename|/etc/conf.d/DAEMON_NAME}}. This is done to separate configuration from logic and to keep a consistent style among daemon scripts.
===Daemon arguments===
+
*Use functions in {{Filename|/etc/rc.d/functions}}.
Arguments to your daemon should be placed in {{Filename|/etc/conf.d/DAEMON_NAME}}
+
*Include at least start, stop and restart.
  
 
===Prototype===
 
===Prototype===
Line 24: Line 23:
 
. /etc/rc.conf
 
. /etc/rc.conf
 
. /etc/rc.d/functions
 
. /etc/rc.d/functions
 +
 +
DAEMON=DAEMON_NAME
 +
ARGS=
  
 
[ -r /etc/conf.d/$DAEMON.conf ] && . /etc/conf.d/$DAEMON.conf
 
[ -r /etc/conf.d/$DAEMON.conf ] && . /etc/conf.d/$DAEMON.conf
  
DAEMON=DAEMON_NAME
 
 
PID=$(pidof -o %PPID $DAEMON)
 
PID=$(pidof -o %PPID $DAEMON)
  
Line 33: Line 34:
 
   start)
 
   start)
 
     stat_busy "Starting $DAEMON daemon"
 
     stat_busy "Starting $DAEMON daemon"
     [ -z "$PID" ] && $DAEMON &>/dev/null
+
     [ -z "$PID" ] && $DAEMON $ARGS &>/dev/null
 
     if [ $? = 0 ]; then
 
     if [ $? = 0 ]; then
 
       add_daemon $DAEMON
 
       add_daemon $DAEMON
Line 68: Line 69:
  
 
==Example==
 
==Example==
{{Tip|{{Filename|/etc/rc.d/}} and {{Filename|/etc/conf.d/}} contain many examples}}
+
The following is an example for ''tftpd''. Look in {{Filename|/etc/rc.d}} for a greater variety.
  
A proper example for tftpd:
+
The configuration file:
 
+
{{File|name=/etc/conf.d/tftpd|content=<nowiki>ARGS="-l -s /var/tftpboot -c"</nowiki>}}
{{File|name=/etc/conf.d/tftpd|content=<nowiki>TFTPD_ARGS="-l -s /var/tftpboot -c"</nowiki>}}
+
  
 +
The actual script:
 
{{File|name=/etc/rc.d/tftpd|content=<nowiki>
 
{{File|name=/etc/rc.d/tftpd|content=<nowiki>
 
#!/bin/bash
 
#!/bin/bash
Line 79: Line 80:
 
. /etc/rc.d/functions
 
. /etc/rc.d/functions
  
TFTPD_ARGS=
+
DAEMON=tftpd
[ -f /etc/conf.d/tftpd ] && . /etc/conf.d/tftpd
+
ARGS=
PID=`pidof -o %PPID /usr/sbin/in.tftpd`
+
[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON
 +
 
 +
PID=$(pidof -o %PPID /usr/sbin/in.tftpd)
  
 
case "$1" in
 
case "$1" in
 
  start)
 
  start)
   stat_busy "Starting TFTPD"
+
   stat_busy "Starting $DAEMON"
   [ -z "$PID" ] && /usr/sbin/in.tftpd ${TFTPD_ARGS}
+
   [ -z "$PID" ] && in.tftpd $ARGS &>/dev/null
   if [ $? -gt 0 ]; then
+
   if [ $? = 0 ]; then
    stat_fail
+
     add_daemon $DAEMON
  else
+
     add_daemon in.tftpd
+
 
     stat_done
 
     stat_done
 +
  else
 +
    stat_fail
 
   fi
 
   fi
 
   ;;
 
   ;;
 
  stop)
 
  stop)
   stat_busy "Stopping TFTPD"
+
   stat_busy "Stopping $DAEMON"
   [ -n "$PID" ] && kill $PID &> /dev/null
+
   [ -n "$PID" ] && kill $PID &>/dev/null
   if [ $? -gt 0 ]; then
+
   if [ $? = 0 ]; then
 +
    rm_daemon $DAEMON
 +
    stat_done
 +
  else
 
     stat_fail
 
     stat_fail
  else
 
    rm_daemon in.tftpd
 
    stat_done
 
 
   fi
 
   fi
 
   ;;
 
   ;;
 
  restart)
 
  restart)
 
   $0 stop
 
   $0 stop
 +
  sleep 1
 
   $0 start
 
   $0 start
 
   ;;
 
   ;;
Line 111: Line 115:
 
   echo "usage: $0 {start|stop|restart}"   
 
   echo "usage: $0 {start|stop|restart}"   
 
esac
 
esac
exit 0
 
 
</nowiki>}}
 
</nowiki>}}
 
==Tips==
 
*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}}
 
*Use functions in /etc/rc.d/functions
 
*Include at least start, stop and restart
 

Revision as of 03:17, 14 January 2011

Summary help replacing me
Writing rc.d (daemon) scripts.
Related
Arch Boot Process
Daemon
rc.conf

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