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

From ArchWiki
Jump to: navigation, search
m
(Prototype: in practice, the abs example is ignored, for good reasons: redundant forking to check pid; stick to existing conventions)
Line 20: Line 20:
  
 
===Prototype===
 
===Prototype===
The {{Package Official|abs}} package contains a basic prototype located at {{Filename|/usr/share/pacman/rc-script.proto}}
 
 
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
 
daemon_name=DAEMON_NAME
 
  
 
. /etc/rc.conf
 
. /etc/rc.conf
 
. /etc/rc.d/functions
 
. /etc/rc.d/functions
. /etc/conf.d/$daemon_name.conf
 
  
get_pid() {
+
[ -r /etc/conf.d/$DAEMON.conf ] && . /etc/conf.d/$DAEMON.conf
pidof -o %PPID $daemon_name
+
 
}
+
DAEMON=DAEMON_NAME
 +
PID=$(pidof -o %PPID $DAEMON)
  
 
case "$1" in
 
case "$1" in
 
   start)
 
   start)
     stat_busy "Starting $daemon_name daemon"
+
     stat_busy "Starting $DAEMON daemon"
 
+
     [ -z "$PID" ] && $DAEMON &>/dev/null
     PID=$(get_pid)
+
    if [ $? = 0 ]; then
    if [ -z "$PID" ]; then
+
       add_daemon $DAEMON
      [ -f /var/run/$daemon_name.pid ] && rm -f /var/run/$daemon_name.pid
+
      stat_done
      # 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
 
       stat_fail
 
       stat_fail
Line 58: Line 43:
 
     fi
 
     fi
 
     ;;
 
     ;;
 
 
   stop)
 
   stop)
     stat_busy "Stopping $daemon_name daemon"
+
     stat_busy "Stopping $DAEMON daemon"
    PID=$(get_pid)
+
     [ -n "$PID" ] && kill $PID &>/dev/null
    # KILL
+
     if [ $? = 0 ]; then
     [ ! -z "$PID" ] && kill $PID &> /dev/null
+
      rm_daemon $DAEMON
    #
+
      stat_done
     if [ $? -gt 0 ]; then
+
    else
 
       stat_fail
 
       stat_fail
 
       exit 1
 
       exit 1
    else
 
      rm -f /var/run/$daemon_name.pid &> /dev/null
 
      rm_daemon $daemon_name
 
      stat_done
 
 
     fi
 
     fi
 
     ;;
 
     ;;
 
 
   restart)
 
   restart)
 
     $0 stop
 
     $0 stop
     sleep 3
+
     sleep 1
 
     $0 start
 
     $0 start
 
     ;;
 
     ;;
 
 
   status)
 
   status)
     stat_busy "Checking $daemon_name status";
+
     stat_busy "Checking $DAEMON status"
     ck_status $daemon_name
+
     ck_status $DAEMON
 
     ;;
 
     ;;
 
 
   *)
 
   *)
 
     echo "usage: $0 {start|stop|restart|status}"
 
     echo "usage: $0 {start|stop|restart|status}"
 
esac
 
esac
 
exit 0
 
 
</pre>
 
</pre>
  

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