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

From ArchWiki
Jump to: navigation, search
m
(Prototype, Tips, etc.)
Line 1: Line 1:
 
[[Category:Boot process (English)]]
 
[[Category:Boot process (English)]]
==Naming scheme==
+
[[Category:HOWTOs (English)]]
The name of the rc.d script should follow the name of the executable, not the name of the package. It must reflect what it's going to do.
+
  
==Requirements==
+
{{Article summary start}}
* If a daemon accepts arguments, the script must have its equivalent in /etc/conf.d.
+
{{Article summary text|How to write rc.d (daemon) scripts.}}
* The rc.d script must have at least three methods, being start, stop and restart.
+
{{Article summary heading|Related}}
 +
{{Article summary wiki|Daemon}}
 +
{{Article summary wiki|rc.conf}}
 +
{{Article summary wiki|Arch_Boot_Process|Arch Boot Process}}
 +
{{Article summary end}}
  
==Example==
+
==Introduction==
A proper example for tftpd:
+
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.
  
/etc/conf.d/tftpd:
+
==Basic scheme==
TFTPD_ARGS="-l -s /var/tftpboot -c"
+
Most rc.d scripts follow the same structure but the details are typically specific to the daemon.
  
/etc/rc.d/tftpd
+
===Daemon arguments===
+
Arguments to your daemon should be placed in {{Filename|/etc/conf.d/DAEMON_NAME}}
#!/bin/bash
+
 
. /etc/rc.conf
+
===Prototype===
. /etc/rc.d/functions
+
The {{Package Official|abs}} package contains a basic prototype located at {{Filename|/usr/share/pacman/rc-script.proto}}
TFTPD_ARGS=
+
 
[ -f /etc/conf.d/tftpd ] && . /etc/conf.d/tftpd
+
<pre>
PID=`pidof -o %PPID /usr/sbin/in.tftpd`
+
#!/bin/bash
case "$1" in
+
 
 +
daemon_name=DAEMON_NAME
 +
 
 +
. /etc/rc.conf
 +
. /etc/rc.d/functions
 +
. /etc/conf.d/$daemon_name.conf
 +
 
 +
get_pid() {
 +
pidof -o %PPID $daemon_name
 +
}
 +
 
 +
case "$1" in
 
   start)
 
   start)
     stat_busy "Starting TFTPD"
+
     stat_busy "Starting $daemon_name daemon"
     [ -z "$PID" ] && /usr/sbin/in.tftpd ${TFTPD_ARGS}
+
 
    if [ $? -gt 0 ]; then
+
     PID=$(get_pid)
      stat_fail
+
    if [ -z "$PID" ]; then
 +
      [ -f /var/run/$daemon_name.pid ] && rm -f /var/run/$daemon_name.pid
 +
      # RUN
 +
      $daemon_name
 +
      #
 +
      if [ $? -gt 0 ]; then
 +
        stat_fail
 +
        exit 1
 +
      else
 +
        echo $(get_pid) > /var/run/$daemon_name.pid
 +
        add_daemon $daemon_name
 +
        stat_done
 +
      fi
 
     else
 
     else
       add_daemon in.tftpd
+
       stat_fail
       stat_done
+
       exit 1
 
     fi
 
     fi
 
     ;;
 
     ;;
 +
 
   stop)
 
   stop)
     stat_busy "Stopping TFTPD"
+
     stat_busy "Stopping $daemon_name daemon"
     [ ! -z "$PID" ] && kill $PID &> /dev/null
+
    PID=$(get_pid)
 +
    # KILL
 +
     [ ! -z "$PID" ] && kill $PID &> /dev/null
 +
    #
 
     if [ $? -gt 0 ]; then
 
     if [ $? -gt 0 ]; then
 
       stat_fail
 
       stat_fail
 +
      exit 1
 
     else
 
     else
       rm_daemon in.tftpd
+
       rm -f /var/run/$daemon_name.pid &> /dev/null
 +
      rm_daemon $daemon_name
 
       stat_done
 
       stat_done
 
     fi
 
     fi
 
     ;;
 
     ;;
 +
 
   restart)
 
   restart)
 
     $0 stop
 
     $0 stop
 +
    sleep 3
 
     $0 start
 
     $0 start
 
     ;;
 
     ;;
 +
 +
  status)
 +
    stat_busy "Checking $daemon_name status";
 +
    ck_status $daemon_name
 +
    ;;
 +
 
   *)
 
   *)
     echo "usage: $0 {start|stop|restart}"   
+
     echo "usage: $0 {start|stop|restart|status}"
  esac
+
esac
  exit 0
+
 
 +
exit 0
 +
</pre>
 +
 
 +
==Example==
 +
 
 +
{{Tip|{{Filename|/etc/rc.d}} and {{Filename|/etc/conf.d}} contain many examples}}
 +
 
 +
 
 +
A proper example for tftpd:
 +
 
 +
{{File|name=/etc/conf.d/tftpd|content=<nowiki>TFTPD_ARGS="-l -s /var/tftpboot -c"</nowiki>}}
 +
 
 +
 
 +
{{File|name=/etc/rc.d/tftpd|content=<nowiki> #!/bin/bash
 +
. /etc/rc.conf
 +
. /etc/rc.d/functions
 +
TFTPD_ARGS=
 +
[ -f /etc/conf.d/tftpd ] && . /etc/conf.d/tftpd
 +
PID=`pidof -o %PPID /usr/sbin/in.tftpd`
 +
case "$1" in
 +
  start)
 +
  stat_busy "Starting TFTPD"
 +
  [ -z "$PID" ] && /usr/sbin/in.tftpd ${TFTPD_ARGS}
 +
  if [ $? -gt 0 ]; then
 +
    stat_fail
 +
  else
 +
    add_daemon in.tftpd
 +
    stat_done
 +
  fi
 +
  ;;
 +
  stop)
 +
  stat_busy "Stopping TFTPD"
 +
  [ ! -z "$PID" ] && kill $PID &> /dev/null
 +
  if [ $? -gt 0 ]; then
 +
    stat_fail
 +
  else
 +
    rm_daemon in.tftpd
 +
    stat_done
 +
  fi
 +
  ;;
 +
restart)
 +
  $0 stop
 +
  $0 start
 +
  ;;
 +
*)
 +
  echo "usage: $0 {start|stop|restart}" 
 +
esac
 +
exit 0
 +
</nowiki>}}
 +
 
 +
==Tips==
 +
*Follow the same structure as other rc.d script
 +
**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
 +
*Create /var/run/DAEMON_NAME.pid
 +
*Include at least start,stop and restart.

Revision as of 02:26, 14 January 2011


Summary help replacing me
How to write rc.d (daemon) scripts.
Related
Daemon
rc.conf
Arch_Boot_Process

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

The Template:Package Official package contains a basic prototype located at Template:Filename

#!/bin/bash

daemon_name=DAEMON_NAME

. /etc/rc.conf
. /etc/rc.d/functions
. /etc/conf.d/$daemon_name.conf

get_pid() {
	pidof -o %PPID $daemon_name
}

case "$1" in
  start)
    stat_busy "Starting $daemon_name daemon"

    PID=$(get_pid)
    if [ -z "$PID" ]; then
      [ -f /var/run/$daemon_name.pid ] && rm -f /var/run/$daemon_name.pid
      # RUN
      $daemon_name
      #
      if [ $? -gt 0 ]; then
        stat_fail
        exit 1
      else
        echo $(get_pid) > /var/run/$daemon_name.pid
        add_daemon $daemon_name
        stat_done
      fi
    else
      stat_fail
      exit 1
    fi
    ;;

  stop)
    stat_busy "Stopping $daemon_name daemon"
    PID=$(get_pid)
    # KILL
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    #
    if [ $? -gt 0 ]; then
      stat_fail
      exit 1
    else
      rm -f /var/run/$daemon_name.pid &> /dev/null
      rm_daemon $daemon_name
      stat_done
    fi
    ;;

  restart)
    $0 stop
    sleep 3
    $0 start
    ;;

  status)
    stat_busy "Checking $daemon_name status";
    ck_status $daemon_name
    ;;

  *)
    echo "usage: $0 {start|stop|restart|status}"
esac

exit 0

Example

Tip: Template:Filename and Template:Filename contain many examples


A proper example for tftpd:

Template:File


Template:File

Tips