commit 2d338407a02307bc950e2f649fcde7f8fbbda991
Author: Elan Ruusamäe <[email protected]>
Date:   Mon May 18 17:44:48 2015 +0300

    daemon(): fix --waittime usage
    
    previous implementation was copy paste from killproc, which assumed pid
    already exists when loop is entered.

 lib/functions | 39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)
---
diff --git a/lib/functions b/lib/functions
index 3e111d9..ec8a139 100644
--- a/lib/functions
+++ b/lib/functions
@@ -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
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/rc-scripts.git/commitdiff/2d338407a02307bc950e2f649fcde7f8fbbda991

_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to