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.
E-mail this to a Friend
Printable Version
You may also be interested in other helpful articles:
- FreeBSD: How To Start / Restart / Stop Network and Routing Service
- Ubuntu / Debian Linux: Services Configuration Tool to Start / Stop System Services
- How to: Linux flush or remove all iptables rules
- BSD start services
- Recover MySQL root password
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


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
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?
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.
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.
Yes, I agree with comment#4, How we program FOO is much more important than a bash script.
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
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.