From a7811e2a99b9b8f41ed367a10cf8268e6c696a36 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 16 Apr 2019 09:15:18 +0000
Subject: [PATCH] SW-610: Fix getting pid in swlevel, by using a dedicated
 function. Works on ccu and lcu now.

---
 MAC/APL/APLCommon/src/swlevel | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/MAC/APL/APLCommon/src/swlevel b/MAC/APL/APLCommon/src/swlevel
index 7c5f8935622..785ffbaf0d2 100755
--- a/MAC/APL/APLCommon/src/swlevel
+++ b/MAC/APL/APLCommon/src/swlevel
@@ -42,10 +42,16 @@ ETCDIR=$LOFARROOT/etc
 OPERBINDIR=/opt/operations/bin
 
 LEVELTABLE=${ETCDIR}/swlevel.conf
-# pidof is in /usr/sbin, usually; this is not added to PATH for non-interactive logins 
-# (in /etc/profile) so explicitly find location of executable pidof now.
-PIDOF=`whereis -b pidof | awk '{print $2}'`
-PGREP=pgrep
+
+# Return the PID of a process or script
+function getpid {
+  PROGRAM="$1"
+
+  # use "pgrep -f" to get the PID of scripts.
+  # only use this as a fall-back when "pgrep" itself returns nothing,
+  # since "pgrep -f" returns false positives for non-scripts.
+  pgrep $PROGRAM || pgrep -f $PROGRAM
+}
 
 # Counter to indicate if 48V reset has been attempted
 has_been_reset=0
@@ -282,7 +288,7 @@ start_prog()
 	fi
 
 	# Check if program is already running
-        $PGREP -f ${prog} 1>/dev/null 2>&1
+        getpid ${prog} 1>/dev/null 2>&1
 	if [ $? -ne 0 ]; then
 		curdate=`date +%Y%m%dT%H%M%S`
 		# WinCC needs special treatment
@@ -349,7 +355,7 @@ stop_prog()
 	fi
 
 	# get processlist
-	$PGREP -f ${prog} 1>/dev/null 2>&1
+	getpid ${prog} 1>/dev/null 2>&1
 	if [ $? -ne 0 ]; then
 		return
 	fi
@@ -370,7 +376,7 @@ stop_prog()
 	fi
 
 	# first try normal kill
-	for pid in `$PGREP -f ${prog}`
+	for pid in `getpid ${prog}`
 	do
 		echo "Softly killing ${prog}(${pid})"
 		$asroot kill $pid 1>/dev/null 2>&1
@@ -378,7 +384,7 @@ stop_prog()
 	done
 
 	# when normal kill did not work, kill is with -9
-	for pid in `$PGREP -f ${prog}`
+	for pid in `getpid ${prog}`
 	do
 		echo "Hard killing ${prog}(${pid})"
 		$asroot kill -9 $pid 1>/dev/null 2>&1
@@ -386,7 +392,7 @@ stop_prog()
 	done
 	# if user0 or lofarsys, try normal kill as root
 
-	for pid in `$PGREP -f ${prog}`
+	for pid in `getpid ${prog}`
 	do
         if [ "$user" == "user0" -o "$user" == "lofarsys" ]; then
 			sudo kill $pid 1>/dev/null 2>&1
@@ -395,7 +401,7 @@ stop_prog()
 	done
 
 	# if user0 or lofarsys, try hard kill as root
-	for pid in `$PGREP -f ${prog}`
+	for pid in `getpid ${prog}`
 	do
 	  if [ "$user" == "user0" -o "$user" == "lofarsys" ]; then
             sudo kill -9 $pid 1>/dev/null 2>&1
@@ -404,7 +410,7 @@ stop_prog()
 	done
 
 	# if still alive, write a message
-	for pid in `$PGREP -f ${prog}`
+	for pid in `getpid ${prog}`
 	do
 	  echo -n "Could not kill ${prog}(${pid}); "
 	  if [ "$user" == "user0" -o "$user" == "lofarsys" ]; then
@@ -464,9 +470,9 @@ status_prog()
 		# find out the processID of the possibly (running) process
 		obsid=()
 		pid_user=()
-		$PGREP -f ${prog} 1>/dev/null 2>&1
+		getpid ${prog} 1>/dev/null 2>&1
 		if [ $? -eq 0 ]; then
-			pid=( `$PGREP -f ${prog}` )
+			pid=( `getpid ${prog}` )
 			i=0
 			for apid in ${pid[@]}
 			do
-- 
GitLab