How to develop an init script
base
is the service name.
/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
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
daemon --user=username command
# Log
SUBSYS=serviceName
LOG_PATH=/var/log
START_LOG=$LOG_PATH/"$SUBSYS"_start.log
STOP_LOG=$LOG_PATH/"$SUBSYS"_stop.log
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
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"