≡ Menu

Linux: How to write a System V init script to start, stop, and restart my own application or service

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 Runlevels in System V describe certain states. For example:

  • Runlevel 0: Halt
  • Runlevel 1: Single user mode
  • Runlevel 6: Reboot

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.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • Sean September 20, 2006, 12:15 pm

    Don’t forget to highlight the description and chkconfig comments at the top, they’re what the chkconfig utility uses to figure out what run levels to start/stop the script in. It’s not necessary for the script to work, but if the comments are in there they should be right!

    Sean

  • Saleem Hardy March 16, 2007, 3:52 am

    Hello, I have a problem and would like some real help. I believe it deals with the topic just discussed. At this point, I’m not sure of the questions I’m supposed to be asking. Here’s my problem: I’m about to start programming some “multiplayer” games for the blind. The games will be text-based games (no heavy load graphics). I am going to write my game interface using my ‘visual studio 2005’ software in c++. All the players will have a copy of my game interface on their computers. The game interface will send each player’s move or ‘turn decision’ to a ‘constantly’ running script, which will be ‘listening’ for clients on the Internet. The script will then send the ‘move’ to all the other players of the game so everyone has the same instance of the game at the same time. I have never done this before. I am just extrapolating on what I like to call the ‘logical progression flow’. I have a domain (htpad.com) with the host4profit website hosting service. They have two files in my root directory: ‘.bash_profile’ and ‘.bashrc’. I know these two files have something to do with the ‘bash’ shell. I was also told I would need to create a ‘deamon bash script’ if I wanted a script that would continuously ‘listen’ on the Internet for clients in order to process their moves. My question is this: Am I on the write track, and how do I create a ‘safe’ deamon script that will not make my host provider nervous?

  • nixCraft March 16, 2007, 7:20 pm

    This is script is an example to start or stop daemon or service. You are coding in C/C++ and you need to write daemon using same language until and unless you are writing API for perl or php.

  • Saleem Hardy March 16, 2007, 10:36 pm

    Is a daemon simply a program that I write in ‘bash’ and is always running on my host’s server and it simply calls my pearl script that does all the ‘ios::socket’ listening? I’m not sure what a daemon is and how to create one. Please explain or give me a link to a text file explaining the daemon file. P.S. Should this be a new topic? If so, where should I post it. I would need to post the original message.

  • ratana April 19, 2007, 5:03 pm

    Yes, I agree with comment#4, How we program FOO is much more important than a bash script.

  • inquisitive April 24, 2007, 8:48 pm

    When I use the script for a test daemon, get the following error for line:45 which the echo statement below. Am I missing something?

    echo $”Usage: $0 {start|stop|restart|reload|status}”

    ./testd: line 45: restart: command not found
    –cmd and –run are incompatible with –string or –name
    ./testd: line 45: reload: command not found
    ./testd: line 45: status}”: command not found

  • Ron August 10, 2007, 10:16 pm

    Inquisitive,

    If you cut and past this example, it may have inserted the wrong ” characters. Simply replace the double quotes in your copy and it should work.

  • Shalom Crown October 28, 2010, 9:31 pm

    I would be interested to know if and how the game for the blind was written.

    For those who are used to J2EE, Apache modules, .NET and all those other technologies, something that does netcat | sed … | netcat might be a refreshing change.

  • Jorge Ramirez December 2, 2010, 6:34 pm

    I get :
    [root@mybox init.d]# service myserviced start
    env: /etc/init.d/myserviced: No such file or directory

    the file does exists and its executable.

    chkconfig myserviced on
    went without complaining? any tips?

    I’m on Centos 5.5.

  • akram January 8, 2012, 2:29 pm

    I will use this script to restart mysql service , so I will change the following :
    /path/to/FOO & –> to be /retrovue/home/mysql
    retrovue ( user )
    touch /var/lock/subsys/FOO –> to be touch /var/lock/subsys/mysql
    killproc FOO —-> to be killproc mysql
    rm -f /var/lock/subsys/FOO –> rm -f /var/lock/subsys/mysql

    is it correct ?

    How I can make this Script run daily at 01:00 AM

    I wait your reply ASAP

  • Shalom Crown January 9, 2012, 6:19 pm

    I haven’t used mysql for a few years, but it should have a System V service script of its own, or use upstart or whatever replaces System V services on your machine.

    You have to specify what operating system, version etc.

    Note also that the above script probably won’t work on Debian based systems as there is no /etc/init.d/functions – and you have to do all those things the slow way.

    Both Fedora and Ubuntu have moved on to Upstart (and I believe Fedora have something even newer), so the System V service scripts become an emulation.

    With System V you would use a command like:
    service restart mysql

    With Upstart this becomes something like:
    restart mysql

    To do this daily at 01:00 you would have to edit your root user’s crontab, and place the commands there – like

    sudo crontab -e

    ..and place a line in the file like:

    00 01 * * * restart mysql

    But then I have questions for you (to think about):
    How long have you been using Unix/Linux?
    If this is a production server – do you need expert advice, rather than doing it on your own?
    Why is there a need to restart a database server every night?

    Especially the last question. I can’t imagine that recent releases of MySQL have memory leaks or other problems. I leave database servers running for months at a time with no problems.
    Maybe the problem is rather that you have long running queries that you don’t control?

  • Pradeep October 13, 2012, 2:20 am

    Hi,

    Does this work by placing in the /etc/init.d or do we have to link them to rc3.d and rc0.d. if so… can you please let me know the detailed steps as well…

    Thanks
    Pradeep

  • Pradeep October 13, 2012, 3:00 am

    Hi,

    Do we need to link these to rc3.d and rc0.d . If so, please let me know the detailed procedure apart from placing this script in /etc.init.d/

    Also please let me know how to check if the service is stopped before the server is down.

    Thanks
    Pradeep

  • Shalom Crown October 20, 2012, 4:20 pm

    Once again, I do’t know what operating system etc., so it’s difficult to answer.

    Both Debian/Ubuntu and Fedora/Redhat/CentOS have ‘chkconfig’ scripts that set up the System V style links. If the init.d script is written correctly (with comments indicating the runlevels required), it is enough to do ‘chkconfig myservice on’.

    (chkconfig might not be installed by default on Ubuntu, use apt-get…)

    If the scripts are written correctly, then the ‘stop’ is invoked before system shutdown (which means – transition to runlevel 0).

  • tech May 11, 2013, 4:54 am

    How we determine the order of execution in naming Start script and Kill script.Like K99xxx. On what basis we are specifying 99

  • Wellington Torrejais da Silva June 27, 2014, 3:34 am

    Thanks!!!