Init - How to develop a SysV Init Script

> Operating System - Kernel (Windows, Unix, Linux) > Linux / Unix > Linux - Boot (Init service system) > Linux - init process (Sysv ?)

1 - About

How to develop an init script

3 - Management

3.1 - Base (service Name)

base

is the service name.

Advertising

3.2 - 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

3.3 - 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

3.3.1 - Library

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

3.4 - Log

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

3.5 - 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"
Advertising