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

From ArchWiki
Jump to: navigation, search
(update templates, see Help:Style)
(add note about learning /etc/rc.d/functions source)
Line 11: Line 11:
 
As part of Arch's 'BSD-style' init, rc.d scripts are used to control the starting, stopping and restarting of [[Daemon|daemons]].
 
As part of Arch's 'BSD-style' init, rc.d scripts are used to control the starting, stopping and restarting of [[Daemon|daemons]].
  
==Guideline==
+
== Guideline ==
*Source {{ic|/etc/rc.conf}}, {{ic|/etc/rc.d/functions}}, and optionally {{ic|/etc/conf.d/DAEMON_NAME}}.
+
* Source {{ic|/etc/rc.conf}}, {{ic|/etc/rc.d/functions}}, and optionally {{ic|/etc/conf.d/DAEMON_NAME}}.
*Arguments and other daemon options should be placed in {{ic|/etc/conf.d/DAEMON_NAME}}. This is done to separate configuration from logic and to keep a consistent style among daemon scripts.
+
* Arguments and other daemon options should be placed in {{ic|/etc/conf.d/DAEMON_NAME}}. This is done to separate configuration from logic and to keep a consistent style among daemon scripts.
*Use functions in {{ic|/etc/rc.d/functions}} instead of duplicating their functionality.
+
* Use functions in {{ic|/etc/rc.d/functions}} instead of duplicating their functionality.
*Include at least start, stop and restart as arguments to the script.
+
* Include at least start, stop and restart as arguments to the script.
*There are some functions provided by {{ic|/etc/rc.d/functions}}:
+
**stat_busy "<message>": set status ''busy'' for printed message (e.g. <nowiki>Starting daemon  [BUSY]</nowiki>)
+
**stat_done: set status ''done'' (e.g. <nowiki>Starting daemon  [DONE]</nowiki>)
+
**stat_fail: set status ''failed'' (e.g. <nowiki>Starting daemon  [FAILED]</nowiki>)
+
**get_pid <program>: get PID of the program
+
**ck_pidfile <PID-file> <program>: check whether PID-file is still valid for the program (e.g. <nowiki>ck_pidfile /var/run/daemon.pid daemon || rm -f /var/run/daemon.pid</nowiki>)
+
**[add|rm]_daemon <program>: add/remove program to running daemons (stored in {{ic|/run/daemons/}})
+
  
==Example==
+
== Available functions ==
 +
* There are some functions provided by {{ic|/etc/rc.d/functions}}:
 +
** {{Ic|stat_busy "''message''"}}: set status ''busy'' for printed message (e.g. <nowiki>Starting daemon  [BUSY]</nowiki>)
 +
** {{Ic|stat_done}}: set status ''done'' (e.g. <nowiki>Starting daemon  [DONE]</nowiki>)
 +
** {{Ic|stat_fail}}: set status ''failed'' (e.g. <nowiki>Starting daemon  [FAILED]</nowiki>)
 +
** {{Ic|get_pid ''program''}}: get PID of the program
 +
** {{Ic|ck_pidfile ''PID-file'' ''program''}}: check whether PID-file is still valid for the program (e.g. <nowiki>ck_pidfile /var/run/daemon.pid daemon || rm -f /var/run/daemon.pid</nowiki>)
 +
** {{Ic|<nowiki>[add|rm]_daemon</nowiki> ''program''}}: add/remove program to running daemons (stored in {{ic|/run/daemons/}})
 +
 
 +
Full list of functions is much longer and most possibilities (like way to control whether or not non-root users can launch daemon) are still undocumented and can be learned only from {{ic|/etc/rc.d/functions}} source. See also {{Ic|man rc.d}}.
 +
 
 +
== Example ==
 
The following is an example for ''crond''. Look in {{ic|/etc/rc.d}} for greater variety.
 
The following is an example for ''crond''. Look in {{ic|/etc/rc.d}} for greater variety.
  

Revision as of 09:11, 20 March 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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.

Guideline

  • Source /etc/rc.conf, /etc/rc.d/functions, and optionally /etc/conf.d/DAEMON_NAME.
  • Arguments and other daemon options should be placed in /etc/conf.d/DAEMON_NAME. This is done to separate configuration from logic and to keep a consistent style among daemon scripts.
  • Use functions in /etc/rc.d/functions instead of duplicating their functionality.
  • Include at least start, stop and restart as arguments to the script.

Available functions

  • There are some functions provided by /etc/rc.d/functions:
    • stat_busy "message": set status busy for printed message (e.g. Starting daemon [BUSY])
    • stat_done: set status done (e.g. Starting daemon [DONE])
    • stat_fail: set status failed (e.g. Starting daemon [FAILED])
    • get_pid program: get PID of the program
    • ck_pidfile PID-file program: check whether PID-file is still valid for the program (e.g. ck_pidfile /var/run/daemon.pid daemon || rm -f /var/run/daemon.pid)
    • [add|rm]_daemon program: add/remove program to running daemons (stored in /run/daemons/)

Full list of functions is much longer and most possibilities (like way to control whether or not non-root users can launch daemon) are still undocumented and can be learned only from /etc/rc.d/functions source. See also man rc.d.

Example

The following is an example for crond. Look in /etc/rc.d for greater variety.

The configuration file:

/etc/conf.d/crond
ARGS="-S -l info"

The actual script:

/etc/rc.d/crond
#!/bin/bash

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

DAEMON=crond
ARGS=

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

PID=$(get_pid $DAEMON)

case "$1" in
 start)
   stat_busy "Starting $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"
   [ -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
   ;;
 *)
   echo "usage: $0 {start|stop|restart}"  
esac