From a7b4e4463e756bf3a77b9bc20dba0e6bc0c88307 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Tue, 29 Mar 2022 18:36:49 +0300
Subject: [PATCH] Get rid of pid file outside state directory

---
 README   |  8 ++++----
 clear.sh |  2 +-
 start.sh | 24 +++++++++++++++++-------
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/README b/README
index 87ad8dc..9657313 100644
--- a/README
+++ b/README
@@ -48,11 +48,11 @@ It is advisable to run it under daemontools'es supervise utility for
 convenience.
 
 run.sh creates temporary directory for each probe and links it to
-$probe/state. Also $probe/pid file is created there. run-script expects
-that it is run from the $probe directory, there is state/, where it can
-store its temporary files. $SGMONDIR are also always set $SGMONSRV.
+$probe/state. run-script expects that it is run from the $probe
+directory, there is state/, where it can store its temporary files.
+$SGMONDIR and $SGMONDIR are also always set too.
 
-You can remove all pid-files and state/-directories with clear.sh.
+You can remove all state directories with clear.sh.
 
 Generate HTML page with information about state of all services and
 their output:
diff --git a/clear.sh b/clear.sh
index 38ad620..18fbae5 100755
--- a/clear.sh
+++ b/clear.sh
@@ -1,3 +1,3 @@
 #!/bin/sh
 
-find . \( -name state -or -name -pid \) -delete -print
+find . -name state -delete -print
diff --git a/start.sh b/start.sh
index 30315dd..7d4baa6 100755
--- a/start.sh
+++ b/start.sh
@@ -4,14 +4,14 @@ stop() {
     trap - HUP PIPE INT QUIT TERM EXIT
     echo ... | mailx -s "Stopping" "$NOTIFY_EMAIL"
     for probe in $probes ; do
-        [ -s $probe/pid ] || continue
-        echo killing ${probe}...
-        pkill -F $probe/pid 2>/dev/null || :
+        [ -s $probe/state/pid ] || continue
+        echo killing ${probe}
+        pkill -F $probe/state/pid 2>/dev/null || :
     done
     for probe in $probes ; do
-        read pid < $probe/pid || :
+        read pid < $probe/state/pid || :
         [ -n "$pid" ] || continue
-        echo waiting ${probe}...
+        echo waiting ${probe}
         wait $pid || :
     done
     echo finished
@@ -25,9 +25,19 @@ probes="$("$SGMONDIR"/probelist.sh)"
 echo $(echo "$probes" | wc -l) probes | mailx -s "Starting" "$NOTIFY_EMAIL"
 for probe in $probes ; do
     cd $probe
-    echo starting ${probe}...
+    echo -n starting ${probe}
     "$SGMONDIR"/run.sh $probe >/dev/null 2>&1 &
-    echo $! > pid || :
+    pid=$!
+    while : ; do
+        if  [ -d state ] ; then
+            break
+        else
+            sleep 1
+            echo -n .
+        fi
+    done
+    echo ""
+    echo $pid > state/pid || :
     cd "$root"
 done
 echo OK
-- 
2.51.0