System V init script

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

A System V (SysV) init script is a shell script for Linux and Unix-like systems that follow the System V initialization scheme. You can find them in /etc/init.d/ or /etc/rc.d/ or /usr/local/etc/rc.d/ directory.

Purpose

These scripts control the system's startup and shutdown of services and daemons. These scripts control the system's startup and shutdown of services and daemons. The scripts are typically written in a command interpreter named /bin/sh or /bin/bash or /sbin/openrc-run. The System V init system follows a hierarchical structure. They are divided into different runlevels such as:

  • Boot
  • Default
  • shutdown
  • sysinit
  • nonetwork
  • network

The different runlevels

Runlevels are a state, defined by the services listed in the SysV /etc/rc.d/rc?.d/ directory, where ? is the number of the runlevel. The following runlevels are defined by default for Linux starting from number 0:

Understanding runlevles in SysV
SysV runlevels number Description
0 Halt or shutdown the Linux system
1 Single-user text mode (useful for maintenance mode)
2 Not used
3 Full multi-user text mode (used on Linux servers)
4 Not used
5 Full multi-user GUI mode. You can use X-based login screen. Useful for Linux desktop
6 Reboot Linux system

Usage

A System V init script typically has the following functions:

  1. Start the service:
  2. Stop the service
  3. Restart the service
  4. Find or print the status of the service

Examples

For example, you can start, stop, restart, or get the status of the Nginx service as follows:

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
/etc/init.d/nginx status

You must be the root user to run init.d scripts.

Example of a System V init script

The following SysV init script is named my-java-app and it is stored in the /usr/local/etc/init.d/ directory. The script supports start, stop, restart, and status function of SysV. The start function starts the /usr/local/bin/my-java-app service, the stop function stops the /usr/local/bin/my-java-app service, the restart function restarts the /usr/local/bin/my-java-app service, and the status function checks the status of the /usr/local/bin/my-java-app service:

#!/bin/sh
# Name: /usr/local/etc/init.d/my-java-app
# Purpose: This script starts, stops, and restarts the "my-java-app" service.
# Author: Vivek Gite
# ------------------------------------------------------------------------------
case "$1" in
  start)
    /usr/local/bin/my-java-app start
    ;;
  stop)
    /usr/local/bin/my-java-app stop
    ;;
  restart)
    /usr/local/bin/my-java-app stop
    /usr/local/bin/my-java-app start
    ;;
  status)
    /usr/local/bin/my-java-app status
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

The main logic of the script is implemented using a case statement that takes the command-line argument passed to the script (e.g., start, stop, restart, or status) and invokes the /usr/local/bin/my-java-app binary to actually start, stop, restart the service. The above is a simple example of a System V init script. But, keep in mind that there are multiple ways to write one and the commands used will differ based on the service being controlled.

How do I use this script ?

To use this script, you would typically place it in the /etc/init.d/ or /usr/loca/etc/init.d/ directory and make it executable. Then, you can use commands like service command or "/usr/local/etc/init.d/my-java-app start" to manage the service's startup and shutdown:

sudo /usr/local/etc/init.d/my-java-app start

## OR ##

sudo service my-java-app start

Lighttpd SysV service example

This is from Ubuntu 16.04 LTS server:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          lighttpd
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Should-Start:      fam
# Should-Stop:       fam
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the lighttpd web server.
# Description:       Fast and smalle webserver with minimal memory footprint
#                    developed with security in mind HTTP/1.1 compliant caching
#                    proxy server.
### END INIT INFO


PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lighttpd
NAME=lighttpd
DESC="web server"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON_OPTS="-f /etc/lighttpd/lighttpd.conf"

test -x $DAEMON || exit 0

set -e

check_syntax()
{
	$DAEMON -t $DAEMON_OPTS > /dev/null || exit $?
}

if [ "$1" != status ]; then
	# be sure there is a /var/run/lighttpd, even with tmpfs
	# The directory is defined as volatile and may thus be non-existing
	# after a boot (DPM §9.3.2)
	if ! dpkg-statoverride --list /var/run/lighttpd >/dev/null 2>&1; then
		install -d -o www-data -g www-data -m 0750 "/var/run/lighttpd"
	fi
fi

. /lib/lsb/init-functions

case "$1" in
    start)
	check_syntax
        log_daemon_msg "Starting $DESC" $NAME
        if ! start-stop-daemon --start --oknodo --quiet \
            --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
        then
            log_end_msg 1
        else
            log_end_msg 0
        fi
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" $NAME
        if start-stop-daemon --stop --retry 30 --oknodo --quiet \
            --pidfile $PIDFILE --exec $DAEMON
        then
            rm -f $PIDFILE
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
    reload|force-reload)
	check_syntax
        log_daemon_msg "Reloading $DESC configuration" $NAME
        if start-stop-daemon --stop --signal INT --quiet \
            --pidfile $PIDFILE --exec $DAEMON \
            --retry=TERM/60/KILL/5
        then
            rm $PIDFILE
            if start-stop-daemon --start --quiet  \
                --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
                log_end_msg 0
            else
                log_end_msg 1
            fi
        else
            log_end_msg 1
        fi
        ;;
    reopen-logs)
        log_daemon_msg "Reopening $DESC logs" $NAME
        if start-stop-daemon --stop --signal HUP --oknodo --quiet \
            --pidfile $PIDFILE --exec $DAEMON
        then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
    restart)
	check_syntax
        $0 stop
        $0 start
        ;;
    status)
        status_of_proc -p "$PIDFILE" "$DAEMON" lighttpd && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0

Is Sys V outdated on Linux?

Most modern Linux distros replaced Sys V with Systemd or OpenRC or other init system. First init system used on Linux systems, but it has been largely replaced by systemd, which is a newer and more flexible init system. Here are Linux distors that uses systemd by default:

  1. Arch
  2. CentOS Stream
  3. Debian
  4. Fedora
  5. RHEL
  6. Ubuntu and many more.

See also

External link