wok view boinc/stuff/boinc-client @ rev 20904
Tiny edits.
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Tue Feb 26 23:14:22 2019 +0200 (2019-02-26) |
parents | 02c70d036ea0 |
children |
line source
1 #!/bin/bash
2 #
3 # BOINC - start and stop the BOINC client daemon on Unix
4 #
5 # Unix start/stop script to run the BOINC client as a daemon at
6 # system startup, as the 'boinc' user (not root!).
7 #
8 # This version should work on unixes that have bash, zsh, or ksh. If
9 # started with another Bourne compatible shell, it will attempt to restart
10 # in bash, zsh, or ksh.
11 #
12 # Metadata for chkconfig and the SUSE equivalent INIT info are included below.
13 #
14 # Usage: boinc { start | stop | status | reload | restart }
15 #
17 # Defaults, which can be overridden by putting new NAME=value lines
18 # in /etc/sysconfig/boinc-client, /etc/default/boinc-client,
19 # /etc/boinc-client.conf, or the same files under ${sysconfdir}
21 ## These 4 installation dirs set by autoconf. ##########################
22 prefix=`dirname /usr/.`
23 exec_prefix=`dirname /usr/.`
24 bindir=`dirname ${exec_prefix}/bin/.`
25 sysconfdir=`dirname ${prefix}/etc/.`
26 ########################################################################
28 # set the basic PATH
29 PATH=/sbin:/bin:/usr/sbin:/usr/bin
30 export PATH
32 # Use ps
33 PS=ps
35 # Name of user to run as:
36 #
37 BOINCUSER=boinc
39 # Working directory. Could be /home/boinc, /var/lib/boinc, etc..
40 # The reason I prefer /var/lib/boinc is that this works best for a
41 # cluster of computers where /home/anything might be shared between machines
42 #
43 BOINCDIR=/var/lib/boinc
45 # Name of the client executable. This is the file "boinc" if you
46 # unpacked the download file boinc_M.mm.rr_i686-pc-linux-gnu.sh,
47 # but I like to rename it and put it in a public place.
48 # (Hint: move boincmgr to /usr/local/bin too so anyone can easily use it).
49 #
50 BOINCEXE_NAME=boinc_client
51 BOINCEXE=${bindir}/${BOINCEXE_NAME}
52 BOINCCMD_NAME=boinccmd
53 BOINCCMD=${bindir}/${BOINCCMD_NAME}
55 # Log files (you should rotate these occasionally)
56 #
57 LOGFILE=/var/log/${BOINCEXE_NAME}.log
58 ERRORLOG=/var/log/${BOINCEXE_NAME}_err.log
60 # PID file
61 PIDFILE=/var/run/${BOINCEXE_NAME}.pid
63 # BOINC options: for the command line when running the client.
64 # Be aware that --allow_remote_gui_rpc opens up your machine to the world!
65 #
66 # Add this option if you want to allow boinc manager connections from remote
67 # machines
68 #BOINCOPTS="--allow_remote_gui_rpc"
69 # Add this option if you want to turn off all logging
70 #BOINCOPTS="--daemon"
71 # Add this option if you want to redirect logging to the files stderrdae.txt
72 # and stdoutdae.txt in BOINCDIR rather than LOGFILE and ERRORLOG
73 #BOINCOPTS="--redirectio"
74 # Add this option if you want to run only when no logins from anywhere are
75 # active
76 #BOINCOPTS="--check_all_logins"
77 # The default is no options.
78 BOINCOPTS="--dir $BOINCDIR"
80 # Subsys lock file ...
81 LOCKFILE=/var/lock/${BOINCEXE_NAME}
83 # su on Linux seems to need this to be set to work properly in a script
84 export TERM=dumb
86 ##
87 # Init script function library. This stuff is Red Hat specific,
88 # but if the functions are not found we create our own simple replacements.
89 # (The idea for replacing the functions comes from OpenAFS. Thanks guys!)
91 if printf "Hello" >/dev/null 2>/dev/null ; then
92 # printf works
93 printcol='printf \033[60G%s'
94 elif echo -en "Hello" >/dev/null 2>/dev/null ; then
95 # echo -en works
96 printcol='echo -en \033[60G'
97 else
98 # no printf make do with echo -n
99 printcol="echo -n .........."
100 fi
101 function echo_success () { $printcol "[OK]" ; }
102 function echo_failure () { $printcol "[FAILED]" ; }
103 function echo_warning () { $printcol "[WARNING]" ; }
104 function killproc() {
105 PID=`local_pidof $1`
106 [ $PID ] && kill $PID
107 }
109 # Define pidof
110 function local_pidof() {
111 pidof -s -o $$ -o $PPID -o %PPID $1
112 }
114 # Define runuser
115 RUNUSER=su
117 # Some additional places to look for executables
118 # (Should do this after init.d/functions and sysconfig/boinc, which sets PATH)
119 export PATH=${PATH}:${exec_prefix}/sbin:${bindir}
122 ## Look for any local configuration settings which override all above
123 ## Note: ./boinc-client.conf and ./boinc.conf are for testing purposes
124 config_files="
125 ./boinc-client.conf
126 ./boinc.conf
127 /etc/boinc-client.conf
128 /etc/boinc.conf
129 none
130 "
132 ## find the correct config file
133 for config_file in $config_files ; do
134 if [ -f ${config_file} ] ; then
135 break;
136 fi
137 done
139 if [ "${config_file}" != "none" ]; then
140 # check whether we are using a deprecated name
141 if [ "x$NOWARNING" != "xyes" -a -z "`echo ${config_file} | grep boinc-client`" ]; then
142 fn=`basename $config_file`
143 dn=`dirname $config_file`
144 newname=`echo $fn | sed 's/boinc/boinc-client/'`
145 echo -n "The filename '${config_file}' is deprecated..."
146 echo_warning
147 echo
148 echo -n "Please rename your config file to '${dn}/${newname}'"
149 echo_warning
150 echo
151 fi
152 # execute the config file.
153 . ${config_file}
154 fi
157 ## Add ${BOINCDIR} to the path, just in case the executables are stored there.
158 export PATH=${PATH}:${BOINCDIR}
160 ## Create the working directory if it doesn't exist:
161 if [ ! -d $BOINCDIR ]; then
162 echo -n "Creating $BOINCDIR "
163 if mkdir -p $BOINCDIR 2>/dev/null ; then
164 if [ -n "$BOINCUSER" ] ; then
165 if chown $BOINCUSER $BOINCDIR ; then
166 echo_success
167 else
168 echo_failure
169 echo
170 exit 7
171 fi
172 fi
173 else
174 echo_failure
175 echo
176 exit 7
177 fi
178 fi
180 ## Check what user we are running as:
181 # we can't rely on the existence of "whoami" or "logname", so we'll use ps.
182 USERNOW=`${PS} u $$ | tail -1 | awk '{print $2}'`
183 if [ -z "$BOINCUSER" ] ; then
184 BOINCUSER="${USERNOW}"
185 fi
187 ## Check that BOINCUSER actually exists
188 if [ -z "`grep ^${BOINCUSER}: /etc/passwd`" ] ; then
189 if [ -z "`ypcat passwd 2>/dev/null | grep ^${BOINCUSER}:`" ] ; then
190 if [ -z "`nidump passwd / 2>/dev/null | grep ^${BOINCUSER}:`" ] ; then
191 echo -n ERROR: user ${BOINCUSER} does not exist.
192 echo_failure
193 echo
194 exit 9
195 fi
196 fi
197 fi
199 # if we are running as root, print a warning.
200 if [ "x$NOWARNING" != "xyes" -a "$BOINCUSER" = "root" ] ; then
201 echo -n WARNING: boinc-client will be running as root
202 echo_warning
203 echo
204 fi
206 # check whether we will be able to write to the BOINC directory
207 if [ "${USERNOW}" = "${BOINCUSER}" ] ; then
208 if [ ! -O ${BOINCDIR} ] ; then
209 echo -n ERROR: $BOINCDIR is not owned by $BOINCUSER.
210 echo_failure
211 echo
212 exit 8
213 fi
214 elif [ "${USERNOW}" = "root" ] ; then
215 cmd="if test -O ${BOINCDIR} ; then echo success ; fi"
216 if [ -z `su $BOINCUSER -c "$cmd"` ]; then
217 echo -n ERROR: $BOINCDIR is not owned by $BOINCUSER.
218 echo_failure
219 echo
220 exit 8
221 fi
222 fi
225 ## Locate the executable, either boinc_client, boinc,
226 ## or boinc_M.mm_.... with highest version number
227 ## We only do this if BOINCEXE set above isn't found or is not executable.
228 if [ ! -x $BOINCEXE ]; then
229 BOINCEXE=`$WHICH $BOINCEXE_NAME 2>/dev/null`
230 if [ ! -x "$BOINCEXE" ]; then
231 BOINCEXE=`$WHICH boinc 2>/dev/null`
232 fi
233 fi
235 if [ ! -x "$BOINCEXE" ]; then
236 echo -n "Cannot find an executable for the BOINC client."
237 echo_failure
238 echo
239 exit 2
240 fi
242 ## boinccmd will probably be in the same place as the boinc_client
243 if [ ! -x $BOINCCMD ]; then
244 BOINCCMD=`$WHICH $BOINCCMD_NAME 2>/dev/null`
245 if [ ! -x "$BOINCCMD" ]; then
246 BOINCCMD=`dirname $BOINCEXE 2>/dev/null`/${BOINCCMD_NAME}
247 fi
248 fi
251 if [ "x$NOWARNING" != "xyes" -a ! -x $BOINCCMD ]; then
252 echo -n "Cannot find the boinccmd executable. Reload will fail."
253 echo_warning
254 echo
255 fi
257 ## Functions: $1 is one of start|stop|status|reload|restart
259 export NOWARNING=no
261 case "$1" in
262 start)
263 cd $BOINCDIR
264 PID=`local_pidof $BOINCEXE_NAME`
266 if [ -f lockfile -o -f $LOCKFILE ] ; then
267 if [ -z "$PID" ] ; then
268 # a lockfile exists, but boinc_client isn't running
269 /bin/rm -f lockfile $LOCKFILE $PIDFILE 2>&1 > /dev/null
270 else
271 echo -n "Another instance of BOINC is running (PID=${PID})."
272 echo_failure
273 echo
274 exit 1
275 fi
276 fi
278 if [ ! -d projects ] ; then
279 echo -n "The BOINC client requires initialization."
280 echo_warning
281 echo
282 fi
284 touch ${LOGFILE} ${ERRORLOG}
285 NOCORE="ulimit -c 0 2>&1 >/dev/null"
286 echo -n "Starting BOINC client as a daemon: "
287 if [ "${BOINCUSER}" = "${USERNOW}" ] ; then
288 # I am BOINCUSER. Just start client as me.
289 $NOCORE
290 $BOINCEXE $BOINCOPTS >>$LOGFILE 2>>$ERRORLOG &
291 else
292 chown ${BOINCUSER} ${LOGFILE} ${ERRORLOG}
293 if [ -f gui_rpc_auth.cfg ] ; then
294 chmod g+r gui_rpc_auth.cfg
295 fi
296 ${RUNUSER} - $BOINCUSER -c "$NOCORE ; $BOINCEXE $BOINCOPTS >>$LOGFILE 2>>$ERRORLOG" 2>/dev/null > /dev/null &
297 fi
298 sleep 3
299 PID=`local_pidof $BOINCEXE_NAME`
300 if [ $PID ]; then
301 echo $PID > $PIDFILE
302 touch $LOCKFILE && echo_success || ( echo_failure ; echo )
303 fi
304 echo
305 ;;
306 stop)
307 cd $BOINCDIR
308 if [ ! -f $PIDFILE -a ! -f lockfile -a ! -f $LOCKFILE ] ; then
309 echo -n "BOINC is not running (no lockfiles found)."
310 echo_success
311 else
312 echo -n "Stopping BOINC client daemon: "
313 if [ -f $PIDFILE ] ; then
314 PID=`cat $PIDFILE`
315 if [ -n "`${PS} $PID | grep $PID`" ] ; then
316 kill `cat $PIDFILE`
317 sleep 5
318 fi
319 echo_success
320 else
321 killproc $BOINCEXE_NAME && echo_success || echo_failure
322 fi
323 # clean up in any case
324 rm -f lockfile 2>/dev/null >/dev/null
325 rm -f $LOCKFILE 2>/dev/null
326 rm -f $PIDFILE 2>/dev/null
327 fi
328 echo
329 ;;
330 reload)
331 if [ ! -f lockfile -a ! -f $LOCKFILE ] ; then
332 echo "BOINC is not running (no lockfiles found) -- starting service."
333 $0 start
334 else
335 $BOINCCMD --read_cc_config >>$LOGFILE 2>>$ERRORLOG && echo_success || $0 restart
336 fi
337 ;;
338 restart)
339 $0 stop
340 $0 start
341 ;;
343 status)
344 PID=`cat $PIDFILE 2>/dev/null`
345 if [ -n "$PID" ]; then
346 # is it still running?
347 if [ -z "`${PS} $PID | grep $PID`" ]; then
348 # not running. Try the other tests.
349 PID=""
350 fi
351 fi
352 if [ -z "$PID" ]; then
353 PID=`local_pidof $BOINCEXE_NAME`
354 fi
355 if [ -z "$PID" ]; then
356 PID=`local_pidof $BOINCEXE`
357 fi
358 if [ -n "$PID" ]; then
359 echo "BOINC client is running (pid $PID)."
360 else
361 if [ -f $BOINCDIR/lockfile -o -f $LOCKFILE ]; then
362 echo "BOINC is stopped but lockfile(s) exist."
363 exit 2
364 else
365 echo "BOINC client is stopped."
366 exit 3
367 fi
368 fi
369 ;;
371 *)
372 echo "Usage: boinc {start|stop|restart|reload|status}"
373 exit 1
374 esac
376 exit
378 #EOF#