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

From ArchWiki
Jump to: navigation, search
(add warning template about initscripts being obsolete and superseded by systemd)
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Boot process]]
#REDIRECT [[SysVinit#Writing rc.d scripts]]
[[Category:Package development]]
[[cs:Writing rc.d scripts]]
[[es:Writing rc.d scripts]]
[[zh-CN:Writing rc.d scripts]]
{{Article summary start}}
{{Article summary text|Writing rc.d daemon scripts.}}
{{Article summary heading|Related}}
{{Article summary wiki|Arch Boot Process}}
{{Article summary wiki|Daemon}}
{{Article summary wiki|rc.conf}}
{{Article summary end}}
{{Warning|Arch only supports [[systemd]]. Arch's old initscripts package is obsolete and is no longer supported. All Arch users need to move to systemd.}}
[[Initscripts]] uses rc.d scripts to used to control the starting, stopping and restarting of [[Daemon|daemons]].
== Guideline ==
* 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.
* 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.
== 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 configuration file:
{{hc|/etc/conf.d/crond|2=<nowiki>ARGS="-S -l info"</nowiki>}}
The actual script:
. /etc/rc.conf
. /etc/rc.d/functions
[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON
PID=$(get_pid $DAEMON)
case "$1" in
  stat_busy "Starting $DAEMON"
  [ -z "$PID" ] && $DAEMON $ARGS &>/dev/null
  if [ $? = 0 ]; then
    add_daemon $DAEMON
    exit 1
  stat_busy "Stopping $DAEMON"
  [ -n "$PID" ] && kill $PID &>/dev/null
  if [ $? = 0 ]; then
    rm_daemon $DAEMON
    exit 1
  $0 stop
  sleep 1
  $0 start
  echo "usage: $0 {start|stop|restart}" 

Latest revision as of 13:14, 20 February 2015