1 | #!/bin/bash |
---|
2 | # |
---|
3 | # SysV init script for NDG Simple CA Web Service |
---|
4 | # |
---|
5 | # P J Kershaw |
---|
6 | # |
---|
7 | # 13/06/06 |
---|
8 | # |
---|
9 | # Copyright (C) 2006 CCLRC & NERC |
---|
10 | # |
---|
11 | # This software may be distributed under the terms of the Q Public License, |
---|
12 | # version 1.0 or later. |
---|
13 | # |
---|
14 | # chkconfig: 2345 99 01 |
---|
15 | # description: NERC Data Grid Simple CA Web Service |
---|
16 | # |
---|
17 | |
---|
18 | # Edit these variables as required |
---|
19 | user="globus" |
---|
20 | ndgSetupFilePath="/usr/local/NDG/ndgSetup.sh" |
---|
21 | configFilePath= |
---|
22 | |
---|
23 | # Command line args e.g. set alternative port number or properties file |
---|
24 | # path. Note security consideration that these args will appear in a ps |
---|
25 | # process listing |
---|
26 | args=-s |
---|
27 | |
---|
28 | # Source function library. |
---|
29 | . /etc/rc.d/init.d/functions |
---|
30 | |
---|
31 | serviceName=${0##*/} |
---|
32 | prog=SimpleCAServer.py |
---|
33 | RETVAL=0 |
---|
34 | |
---|
35 | |
---|
36 | start() |
---|
37 | { |
---|
38 | echo -n "Starting ${serviceName}: " |
---|
39 | |
---|
40 | # Source NDG Environment |
---|
41 | if [ -f ${ndgSetupFilePath} ]; then |
---|
42 | . ${ndgSetupFilePath} |
---|
43 | else |
---|
44 | failure $"Set-up of NDG environment" |
---|
45 | return |
---|
46 | fi |
---|
47 | |
---|
48 | cmd="cat ${configFilePath} | ${NDG_DIR}/bin/${prog} ${args}" |
---|
49 | |
---|
50 | |
---|
51 | # See if it's already running. Look *only* at the pid file. |
---|
52 | local pid= |
---|
53 | if [ -f /var/run/${serviceName}.pid ]; then |
---|
54 | local line p |
---|
55 | read line < /var/run/${serviceName}.pid |
---|
56 | for p in $line ; do |
---|
57 | [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" |
---|
58 | done |
---|
59 | fi |
---|
60 | |
---|
61 | [ -n "${pid:-}" ] && return |
---|
62 | |
---|
63 | # Make sure it doesn't core dump anywhere; while this could mask |
---|
64 | # problems with the daemon, it also closes some security problems |
---|
65 | ulimit -S -c 0 >/dev/null 2>&1 |
---|
66 | |
---|
67 | # Echo daemon |
---|
68 | [ "${BOOTUP:-}" = "verbose" -a -z "$LSB" ] && echo -n " ${serviceName}" |
---|
69 | |
---|
70 | # And start it up. |
---|
71 | initlog $INITLOG_ARGS -c "su -s /bin/bash - ${user} -c \"${cmd}\"" |
---|
72 | RETVAL=$? |
---|
73 | |
---|
74 | if [ $RETVAL = 0 ]; then |
---|
75 | # Get the process ID |
---|
76 | pid=(`ps --no-headers -u ${user} -o pid,cmd --sort=-pid|grep "$prog"`) |
---|
77 | echo ${pid} > /var/run/${serviceName}.pid |
---|
78 | touch /var/lock/subsys/${serviceName} |
---|
79 | success $"${serviceName} startup" |
---|
80 | else |
---|
81 | failure $"${serviceName} startup" |
---|
82 | fi |
---|
83 | |
---|
84 | echo |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | stop() |
---|
89 | { |
---|
90 | echo -n "Shutting down ${serviceName}: " |
---|
91 | |
---|
92 | # Find pid |
---|
93 | pid= |
---|
94 | if [ -f /var/run/${serviceName}.pid ]; then |
---|
95 | local line p |
---|
96 | read line < /var/run/${serviceName}.pid |
---|
97 | for p in $line ; do |
---|
98 | [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p" |
---|
99 | done |
---|
100 | fi |
---|
101 | |
---|
102 | if [ -z "$pid" ]; then |
---|
103 | failure $"${serviceName} stop - no process found from PID file" |
---|
104 | return |
---|
105 | fi |
---|
106 | |
---|
107 | # Kill it. |
---|
108 | if [ -n "${pid:-}" ] ; then |
---|
109 | [ "$BOOTUP" = "verbose" -a -z "$LSB" ] && echo -n "${serviceName} " |
---|
110 | |
---|
111 | if checkpid $pid 2>&1; then |
---|
112 | # TERM first, then KILL if not dead |
---|
113 | kill -TERM $pid |
---|
114 | usleep 100000 |
---|
115 | if checkpid $pid && sleep 1 && |
---|
116 | checkpid $pid && sleep 3 && |
---|
117 | checkpid $pid ; then |
---|
118 | kill -KILL $pid |
---|
119 | usleep 100000 |
---|
120 | fi |
---|
121 | checkpid $pid |
---|
122 | RC=$? |
---|
123 | [ "$RC" -eq 0 ] && failure $"${serviceName} shutdown" || \ |
---|
124 | success $"${serviceName} shutdown" |
---|
125 | RC=$((! $RC)) |
---|
126 | fi |
---|
127 | else |
---|
128 | failure $"${serviceName} shutdown" |
---|
129 | RC=1 |
---|
130 | fi |
---|
131 | |
---|
132 | # Remove pid file if any. |
---|
133 | rm -f /var/run/${serviceName}.pid |
---|
134 | |
---|
135 | echo |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | restart() |
---|
140 | { |
---|
141 | stop |
---|
142 | start |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | status() |
---|
147 | { |
---|
148 | local pid= |
---|
149 | local pidFilePath=/var/run/${serviceName}.pid |
---|
150 | |
---|
151 | # Get pid from "/var/run/*.pid" file |
---|
152 | if [ -f $pidFilePath ] ; then |
---|
153 | read pid < $pidFilePath |
---|
154 | if [ -z "$pid" ]; then |
---|
155 | echo $"Can't get pid from pid file $pidFilePath" |
---|
156 | return |
---|
157 | fi |
---|
158 | else |
---|
159 | echo $"No pid file $pidFilePath found" |
---|
160 | return |
---|
161 | fi |
---|
162 | |
---|
163 | # look for pid in listing |
---|
164 | local pidFound= |
---|
165 | for i in `pidof -o $$ -o $PPID -o %PPID -x "python"`; do |
---|
166 | [[ $i = $pid ]] && pidFound=Yes && break; |
---|
167 | done |
---|
168 | |
---|
169 | if [ -n "$pidFound" ]; then |
---|
170 | echo $"$prog (pid $pid) is running..." |
---|
171 | return |
---|
172 | |
---|
173 | elif [ -f /var/lock/subsys/${serviceName} ]; then |
---|
174 | echo $"$prog dead but subsys locked and pid file $pidFilePath exists" |
---|
175 | return |
---|
176 | else |
---|
177 | echo $"$prog dead but pid file $pidFilePath exists" |
---|
178 | fi |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | case "$1" in |
---|
183 | start) |
---|
184 | start |
---|
185 | ;; |
---|
186 | stop) |
---|
187 | stop |
---|
188 | ;; |
---|
189 | status) |
---|
190 | # Use status defined in functions file |
---|
191 | status |
---|
192 | ;; |
---|
193 | restart) |
---|
194 | restart ${serviceName} |
---|
195 | ;; |
---|
196 | condrestart) |
---|
197 | if [ -f /var/run/${serviceName}.pid ] ; then |
---|
198 | stop |
---|
199 | start |
---|
200 | fi |
---|
201 | ;; |
---|
202 | *) |
---|
203 | echo \ |
---|
204 | "Usage: ${serviceName} {start|stop|restart|condrestart|status}" |
---|
205 | exit 1 |
---|
206 | ;; |
---|
207 | esac |
---|