How to create an Init script on Centos 6

启动脚本的样例位于 “/usr/share/doc/initscripts-9.03.40/sysvinitfiles” in CentOS 6.5

#!/bin/sh
#
#  
#
# chkconfig:     
# description: 

### BEGIN INIT INFO
# Provides: 
# Required-Start: 
# Required-Stop: 
# Should-Start: 
# Should-Stop: 
# Default-Start: 
# Default-Stop: 
# Short-Description: 
# Description:      
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

exec="/path/to/"
prog=""
config=""

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    # if not running, start it up here, usually something like "daemon $exec"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

让我们来设置一些变量:

#path to our backup script
exec="/path/to/backup"
#name of our script
prog="backup"

The file etc/init.d/functions has a set of functions like daemon and killproc which we will use in our case statement to start and stop our script.

You need to define your start and stop functions, which will basically start your script(here you can use the daemon function) and kill it (killproc). A status function is already available, all you need to do is call the function with the name of your script ($prog).

#!/bin/bash
#Author Leo G
# backup daemon

# chkconfig: 2345 20 80
#The above indicates that the script should be started in levels 2, 3, 4, and 5, #that its start priority should be 20, and that its stop priority should be 80. 
# description: syncs two files
# Source function library.
. /etc/rc.d/init.d/functions
#Define variables
exec="/path/to/backup"
prog="backup"

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    # use daemon to start the service 
        daemon $exec &
    retval=$?
    echo
     return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # use kill proc to stop the service
        killproc $prog -TERM
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        #Only if not running,  start
        rh_status_q && echo "process already started" && exit 0
        $1
        ;;
    stop)
        # Only if service is running stopt
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2

esac
exit $?

测试脚本:

[[email protected]]$ ./initscript.bash start
Starting backup:  OK

测试后台运行:

[[email protected]]$ ./initscript.bash status
backup (pid 2023) is running...

测试关闭

[[email protected]]$ ./initscript.bash stop
Stopping backup:    OK

你可以把脚本放在 /etc/ini.d/yourscriptname, 并使它成为一个服务.

Systemd 是一个更好的方案, Systemd 从 CentOS 7 之后引入.

你可以增加脚本到 “/etc/systemd/system/backup.service”

[Unit]
Description=Backup daemon


[Service]
Type=simple
ExecStart=/path/to/backup

[Install]
WantedBy=multi-user.target

In the Unit section, I have added the description for the script.

In the service section, I have defined the executable i want to run with “ExecStart” and used “type=simple” since my process does not fork another service.

In the install section I added “WantedBy=multi-user.target”, This indicates the runlevel, multi-user.target is runlevel 3.

I recommend you read http://0pointer.de/blog/projects/systemd-for-admins-3.html for a more detailed information, and any suggestions are welcome.

Reload systemd

[[email protected]]$ systemctl daemon-reload

Activate the service

[[email protected]]$ systemctl start backup.service

Check the status

[[email protected]]$systemctl status backup.service.

stop the service via systemctl

[[email protected]]$ systemctl stop backup.service.

Enable service at boot

[[email protected]]$systemctl enable backup.service.

I have also used trap as follows in the backup script itself to ensure that all child processes are killed. You may want to modify your script accordingly.

trap 'kill -HUP 0' EXIT

If you are a system administrator then I recommend you to read the Devops Wiki at https://github.com/Leo-G/DevopsWiki for a list of Linux programming and system administration guides.

Another good article that will help you on CentOS is How to Build an RPM.

As always feedback and suggestions are welcome.

source http://fedoraproject.org/wiki/Packaging:SysVInitScript
http://0pointer.de/blog/projects/systemd-for-admins-3.html