How to Write a Service Script
Script location
/etc/init.d
Header
xxx
is the script name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #!/bin/sh
#
# /etc/init.d/xxx
# Subsystem file for "xxx" server
#
#
# chkconfig: 2345 95 05
# description: xxx server daemon
#
# processname: xxx
# config: /etc/xxx/xxx.conf // Config file
# config: /etc/sysconfig/xxx // config file
# pidfile: /var/run/xxx.pid
### BEGIN INIT INFO
# Provides: xxx
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon
### END INIT INFO
|
Inclde required files
1
2
3
4
5
| # source function library
[ -f /etc/sysconfig/xxx ] && . /etc/rc.d/init.d/functions # include many third-party libraries
# configuration
[ -f /etc/sysconfig/xxx ] && . /etc/sysconfig/xxx
|
Complete Required function: start, stop, restart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| RETVAL=0
prog="xxx"
start() {
echo -n $"Starting $prog:"
RETVAL=$?
echo
}
stop() {
echo -n $"Stopping $prog:"
killproc $prog -TERM
RETVAL=$?
echo
}
reload() {
echo -n $"Reloading $prog:"
killproc $prog -HUP
RETVAL=$?
echo
}
case "$1" in (9)
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
RETVAL=1
esac
exit $RETVAL
|
Update the symbolic links in /etc/rc.d/rcN.d
OR
Put this script into associate /etc/rc.d/rcN.d folder
Run the script manually
1
| service xxx start|status|reload|stop
|
OR
1
| /etc/init.d/xxx start|status|reload|stop
|