Init - How to develop a SysV Init Script

About

How to develop an init script

Management

Base (service Name)

base

is the service name.

Lock

/var/lock/subsys/ contains the lock files created by the init scripts

# SUBSYS must match the script name, as it is used for the process lockfile
# one service can have only one file and it must match the init script name
SUBSYS=serviceName
LOCK_FILE=/var/lock/subsys/$SUBSYS

Functions

Library: On Redhat, a library /etc/init.d/functions contains the standard shared functions for all services script

Example:

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
      . /etc/rc.d/init.d/functions
fi

/etc/sysconfig/init content which is used in /etc/init.d/functions

# color => new RH6.0 bootup
# verbose => old-style bootup
# anything else => new style bootup without ANSI colors or positioning
BOOTUP=color
# Turn on graphical boot
GRAPHICAL=yes
# column to start "[  OK  ]" label in
RES_COL=60
# terminal sequence to move to that column. You could change this
# to something like "tput hpa ${RES_COL}" if your terminal supports it
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
# terminal sequence to set color to a 'success' color (currently: green)
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
# terminal sequence to set color to a 'failure' color (currently: red)
SETCOLOR_FAILURE="echo -en \\033[0;31m"
# terminal sequence to set color to a 'warning' color (currently: yellow)
SETCOLOR_WARNING="echo -en \\033[0;33m"
# terminal sequence to reset to the default color.
SETCOLOR_NORMAL="echo -en \\033[0;39m"
# default kernel loglevel on boot (syslog will reset this)
LOGLEVEL=3
# Set to anything other than 'no' to allow hotkey interactive startup...
PROMPT=yes
# Set to 'yes' to allow probing for devices with swap signatures
AUTOSWAP=no

where the terminal color definition are ansi escape sequence. See Terminal - Color

Library

  • the daemon let you start a service as another user
daemon --user=username command

Log

# Log
SUBSYS=serviceName
LOG_PATH=/var/log
START_LOG=$LOG_PATH/"$SUBSYS"_start.log
STOP_LOG=$LOG_PATH/"$SUBSYS"_stop.log

Pid

The pidfile contains one PID by process started.

Pid file are located in /var/run* with the following structure /var/run/base.pid. See Base

There is two functions in the library

  • __proc_pids {program} [pidfile]. Set pid to pids from /var/run* for {program}. pid should be declared local in the caller. Returns LSB exit code for the 'status' action.
  • __pids_pidof(). Output PIDs of matching processes, found using pidof
  • pidfileofproc() A function to find the pid of a program. Looks *only* at the pidfile pidfileofproc {program}
  • pidofproc() A function to find the pid of a program. pidofproc [-p pidfile] {program}
read line < "$pid_file"
for p in $line ; do
	[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
done
if [ -n "$pid" ]; then
    return 0
fi
return 1 # "Program is dead and /var/run pid file exists"





Discover More
Init - SysV init Script

The init script are bash script that are run by the init daemon. see See sudo Each script will be run as a command and the structure of the command will look like: Where command is the actual...



Share this page:
Follow us:
Task Runner