From 2d338407a02307bc950e2f649fcde7f8fbbda991 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Elan=20Ruusam=C3=A4e?= Date: Mon, 18 May 2015 17:44:48 +0300 Subject: [PATCH] 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 3e111d99..ec8a139d 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 -- 2.44.0