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

From ArchWiki
Jump to: navigation, search
m (decrease nesting)
(Example: this is what happens when the init system itself promotes idiotic copy pasting)
Line 77: Line 77:
 
{{File|name=/etc/rc.d/tftpd|content=<nowiki>
 
{{File|name=/etc/rc.d/tftpd|content=<nowiki>
 
#!/bin/bash
 
#!/bin/bash
 +
 
. /etc/rc.conf
 
. /etc/rc.conf
 
. /etc/rc.d/functions
 
. /etc/rc.d/functions
Line 82: Line 83:
 
DAEMON=tftpd
 
DAEMON=tftpd
 
ARGS=
 
ARGS=
 +
 
[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON
 
[ -r /etc/conf.d/$DAEMON ] && . /etc/conf.d/$DAEMON
  
PID=$(pidof -o %PPID /usr/sbin/in.tftpd)
+
PID=$(pidof -o %PPID in.tftpd)
  
 
case "$1" in
 
case "$1" in
Line 95: Line 97:
 
   else
 
   else
 
     stat_fail
 
     stat_fail
 +
    exit 1
 
   fi
 
   fi
 
   ;;
 
   ;;
Line 105: Line 108:
 
   else
 
   else
 
     stat_fail
 
     stat_fail
 +
    exit 1
 
   fi
 
   fi
 
   ;;
 
   ;;

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