]> git.pld-linux.org Git - projects/rc-scripts.git/commitdiff
daemon(): fix --waittime usage
authorElan Ruusamäe <glen@delfi.ee>
Mon, 18 May 2015 14:44:48 +0000 (17:44 +0300)
committerElan Ruusamäe <glen@delfi.ee>
Mon, 18 May 2015 14:46:05 +0000 (17:46 +0300)
previous implementation was copy paste from killproc, which assumed pid
already exists when loop is entered.

lib/functions

index 3e111d99b3fd6a83f81e4f9a49b65387636885d3..ec8a139dbacad3ffb59b9822be01ec7e6ef3c392 100644 (file)
@@ -782,18 +782,10 @@ daemon() {
                fi
                ); then
 
-               if [ -n "$waitname" -a -n "$waittime" ]; then
-                       # Save basename.
-                       base=${waitname##*/}
-                       # Find pid.
-                       pid=$(pidofproc "$waitname" "$pidfile")
-                       [ -z "$pid" ] && pid=$(pidofproc "$base" "$pidfile")
-                       i=0
-                       while [ "$i" -lt "$waittime" ]; do
-                               i=$((i + 1))
-                               checkpid $pid && break
-                               sleep 1
-                       done
+               # wait for process (or pidfile) to be created
+               if [ "$waittime" -gt 0 ]; then
+                       # waitname can be empty, as if pidfile is in use, it is not relevant
+                       waitproc "$waittime" "$waitname" "$pidfile"
                fi
                log_success "$1 startup"
                ok
@@ -806,6 +798,29 @@ daemon() {
        return $exit_code
 }
 
+# wait (in seconds) for process (or pidfile) to be created
+# example: waitproc 30 httpd /var/run/httpd.pid
+waitproc() {
+       local waittime=$1 procname=$2 pidfile=$3
+       local i=0 pid
+       local now=$(date +%s)
+       local maxtime=$(($now + $waittime))
+
+       if [ -z "$procname" -a -z "$pidfile" ]; then
+               msg_usage "waitproc: procname or pidfile must be specified"
+               return 2
+       fi
+
+       while [ "$(date +%s)" -lt "$maxtime" ]; do
+               pid=$(pidofproc "$procname" "$pidfile")
+               [ -n "$pid" ] && break
+
+               # start-stop-daemon uses same delay
+               usleep 20000
+               i=$(( i + 1 ))
+       done
+}
+
 # A function to stop a program.
 killproc() {
        local notset killlevel base pid pidfile result delay=3 try
This page took 0.172394 seconds and 4 git commands to generate.