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

by Vivek Gite on September 19, 2006 · 10 comments

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 10 comments… read them below or add one }

1 Sean September 20, 2006

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

Reply

2 Saleem Hardy March 16, 2007

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?

Reply

3 nixcraft March 16, 2007

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.

Reply

4 Saleem Hardy March 16, 2007

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.

Reply

5 ratana April 19, 2007

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

Reply

6 inquisitive April 24, 2007

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

Reply

7 Ron August 10, 2007

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.

Reply

8 Shalom Crown October 28, 2010

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.

Reply

9 akram January 8, 2012

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

Reply

10 Shalom Crown January 9, 2012

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?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">



Previous post:

Next post: