System V init script to start, stop, and restart own application or service


To totally unlock this section you need to Log-in


Login

System V (abbreviated as SysV) is most widely used across most Linux distributions. But what is System V?

Init is the program on Unix and Linux systems which spawns all other processes. It runs as a daemon and typically has PID 1. It is the parent of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab file. The main advantages is flexibility and scalability provided by SysV.

The Run Levels in System V describe certain states. For example:

Run-level 0: Halt the system.
Run-level 1: Single-user mode (for special administration).
Run-level 2: Local Multiuser with Networking but without network service (like NFS)
Run-level 3: Full Multiuser with Networking
Run-level 4: Not Used
Run-level 5: Full Multiuser with Networking and X Windows(GUI)
Run-level 6: Reboot.

A runlevel is a state of init and the whole system that defines what system services are operating. Run levels are identified by numbers. Some system administrators use run levels to define which subsystems are working, e.g., whether X is running, whether the network is operational, and so on.

Others have all subsystems always running or start and stop them individually, without changing run levels, since run levels are too coarse for controlling their systems. You need to decide for yourself, but it might be easiest to follow the way your Linux distribution does things.

All System V init scripts are stored in /etc/rc.d/init.d/ or /etc/init.d directory. These scripts are used to control system startup and shutdown. Usually you will find scripts to start a web server or networking. For example you type the command:

# /etc/init.d/httpd start

OR
# /etc/init.d/network restart

In above example httpd or network are System V scripts written in bash or sh shell. Here is a sample shell script:

#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service FOO
start() {
        initlog -c "echo -n Starting FOO server: "
        /path/to/FOO &
        ### Create the lock file ###
        touch /var/lock/subsys/FOO
        success $"FOO server startup"
        echo
}
# Restart the service FOO
stop() {
        initlog -c "echo -n Stopping FOO server: "
        killproc FOO
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/FOO
        echo
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status FOO
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0

Above script is specific to Cent OS or Fedora Core/Red Hat linux. But it should work on all other Linux distro as well. Make sure you replace the FOO name (word/path start with FOO) with actual application name.