OSDN Git Service

Merge branch 'dev'
[pysilhouette/pysilhouette.git.git] / doc / rc.d / init.d / silhouetted
1 #!/bin/bash
2 #
3 # silhouetted  The startup script for the Pysilhouette system.
4 #
5 # chkconfig: 2345 97 03
6 # description: Pysilhouette is an application running in the background system.
7 #
8 # processname: silhouetted
9 # config: /etc/sysconfig/silhouetted
10 # pidfile: /var/run/silhouetted.pid
11 #          /var/run/schedulerd.pid
12 #          /var/run/performerd.pid
13 #          /var/run/asynperformerd.pid
14 #          /var/run/asynschedulerd.pid
15 #
16 # lockfile: /var/lock/subsys/silhouetted
17 #           /var/lock/subsys/schedulerd
18 #           /var/lock/subsys/performerd
19 #           /var/lock/subsys/asynperformerd
20 #           /var/lock/subsys/asynschedulerd
21 #
22
23 source /etc/rc.d/init.d/functions
24 source /etc/sysconfig/network
25
26 # For SELinux we need to use 'runuser' not 'su'
27 if [ -x /sbin/runuser ]; then
28     SU=runuser
29 else
30     SU=su
31 fi
32
33 # Check that networking is up.
34 [ ${NETWORKING} = "no" ] && exit 1
35
36 #Default value
37 prog="silhouette"
38 progd="silhouetted"
39 app="pysilhouette"
40 sch_progd='schedulerd'
41 per_progd='performerd'
42 asyn_sch_progd='asynschedulerd'
43 asyn_per_progd='asynperformerd'
44
45 sysconfig="/etc/sysconfig/${progd}"
46
47 # Read configuration
48 [ -r "${sysconfig}" ] && source "${sysconfig}"
49
50 if [ "x${PYTHON}" == "x" ]; then
51   PYTHON=`which python`
52 fi
53
54 # Config file.
55 conf="/etc/opt/${app}/${prog}.conf"
56
57 # Process id file.
58 pidfile="/var/run/${progd}.pid"
59 lockfile="/var/lock/subsys/${progd}"
60 sch_pidfile="/var/run/${sch_progd}.pid"
61 sch_lockfile="/var/lock/subsys/${sch_progd}"
62 per_pidfile="/var/run/${per_progd}.pid"
63 per_lockfile="/var/lock/subsys/${per_progd}"
64 asyn_sch_pidfile="/var/run/${asyn_sch_progd}.pid"
65 asyn_sch_lockfile="/var/lock/subsys/${asyn_sch_progd}"
66 asyn_per_pidfile="/var/run/${asyn_per_progd}.pid"
67 asyn_per_lockfile="/var/lock/subsys/${asyn_per_progd}"
68
69 # Daemon mode.
70 extra_args=""
71 if [ "x${DAEMON}" = "xyes" ]; then
72     extra_args=${extra_args}" -d"
73 fi
74
75 # Debug mode.
76 if [ "x${DEBUG}" = "xyes" ]; then
77     extra_args=${extra_args}" -v"
78 fi
79
80 desc="${progd} (Daemon)"
81
82 # options
83 CMD_ARGS="-p ${pidfile} -c ${conf} ${extra_args}"
84
85
86 start() {
87     echo -n $"Starting $desc: "
88     if [ -e ${pidfile} ]; then
89             echo "already running..."
90             return 1
91     fi
92
93     touch ${pidfile} ${sch_pidfile} ${per_pidfile} ${asyn_sch_pidfile} ${asyn_per_pidfile}
94     chown ${USER}:${GROUP} ${pidfile} ${sch_pidfile} ${per_pidfile} ${asyn_sch_pidfile} ${asyn_per_pidfile}
95     if [ "x${PYTHON_SEARCH_PATH}" != "x" ]; then
96         env="PYTHONPATH=${PYTHON_SEARCH_PATH}:\$PYTHONPATH"
97     fi
98     ${SU} -l ${USER} -c "${env} ${PYTHON} ${PREFIX}/opt/pysilhouette/bin/${prog}.py ${CMD_ARGS}"
99     RETVAL=$?
100     if [ ${RETVAL} -eq 0 ]; then
101         touch ${lockfile} ${sch_lockfile} ${per_lockfile} ${asyn_sch_lockfile} ${asyn_per_lockfile}
102         success
103     else
104         failure 
105         stop
106     fi
107     echo ""
108     return ${RETVAL} 
109 }
110
111 silhouetted_stop() {
112     echo -n $"Shutting down $desc: "
113     if [ ! -e ${pidfile} ]; then
114         echo "not running..."
115         return 1
116     fi
117     pid=`cat ${pidfile}`
118     if [ "x${pid}" == "x" ]; then
119         echo "not running... - not pid"
120         rm -f ${pidfile}
121         return 1
122     fi
123     killproc -p ${pidfile} -15
124     echo
125     RETVAL=$?
126     return ${RETVAL}
127 }
128
129 stop() {
130     silhouetted_stop
131     SIL_RETVAL=$?
132     if [ ${SIL_RETVAL} -eq 0 ]; then
133         rm -f ${lockfile}
134         rm -f ${pidfile}
135     fi
136
137     # scheduler
138     eval "/etc/rc.d/init.d/${sch_progd} stop"
139     SCH_RETVAL=$?
140     if [ ${SCH_RETVAL} -eq 0 ]; then
141         rm -f ${sch_lockfile}
142         rm -f ${sch_pidfile}
143     fi
144
145     # performer
146     eval "/etc/rc.d/init.d/${per_progd} stop"
147     PER_RETVAL=$?
148     if [ ${PER_RETVAL} -eq 0 ]; then
149         rm -f ${per_lockfile}
150         rm -f ${per_pidfile}
151     fi
152
153     # asynscheduler
154     eval "/etc/rc.d/init.d/${asyn_sch_progd} stop"
155     ASYN_SCH_RETVAL=$?
156     if [ ${ASYN_SCH_RETVAL} -eq 0 ]; then
157         rm -f ${asyn_sch_lockfile}
158         rm -f ${asyn_sch_pidfile}
159     fi
160
161     # asynperformer
162     eval "/etc/rc.d/init.d/${asyn_per_progd} stop"
163     ASYN_PER_RETVAL=$?
164     if [ ${ASYN_PER_RETVAL} -eq 0 ]; then
165         rm -f ${asyn_per_lockfile}
166         rm -f ${asyn_per_pidfile}
167     fi
168
169     # The return code of the performer daemon is the first digit. 
170     # The return code of the scheduler daemon is the second digit. 
171     # The return code of the silhouetted daemon is the third digit. 
172     # All stop functions return only the exit code of 0(Normal) or 1(Abnormal).
173     #RETVAL=`expr ${SIL_RETVAL} \* 100 + ${SCH_RETVAL} \* 10 + ${PER_RETVAL}`
174     RETVAL=`expr ${SIL_RETVAL} \* 10000 + ${SCH_RETVAL} \* 1000 + ${PER_RETVAL} \* 100 + ${ASYN_SCH_RETVAL} \* 10 + ${ASYN_PER_RETVAL}`
175     return ${RETVAL}
176 }
177
178 restart() {
179     stop
180     sleep 1
181     start
182 }
183
184
185 case "$1" in
186     start)
187         start
188         ;;
189     stop)
190         stop
191         ;;
192     restart|reload)
193         restart
194         ;;
195     condrestart)
196         [ -e ${lockfile} ] && restart
197         RETVAL=$?
198         ;;
199     status)
200         status ${progd}
201     eval "/etc/rc.d/init.d/${sch_progd} status"
202     eval "/etc/rc.d/init.d/${per_progd} status"
203     eval "/etc/rc.d/init.d/${asyn_sch_progd} status"
204     eval "/etc/rc.d/init.d/${asyn_per_progd} status"
205         RETVAL=$?
206         ;;
207     *)
208         echo $"Usage: $0 {start|stop|restart|condrestart|status}"
209         RETVAL=1
210 esac
211
212 exit $RETVAL