From a25152caef44f60f2e2fabec8c14ad77b6f92447 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Tue, 15 Jan 2019 13:25:12 +0000
Subject: [PATCH] Avoid use of pgrep

---
 src/freedombone-app-gnusocial   |  3 ++-
 src/freedombone-app-postactiv   |  3 ++-
 src/freedombone-base-email      |  2 +-
 src/freedombone-ci              |  3 ++-
 src/freedombone-image-customise |  2 +-
 src/freedombone-installer       | 11 +++++++----
 src/freedombone-mesh-install    |  2 +-
 src/freedombone-utils-database  |  9 ++++++---
 8 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/freedombone-app-gnusocial b/src/freedombone-app-gnusocial
index 98dd38806..711cae165 100755
--- a/src/freedombone-app-gnusocial
+++ b/src/freedombone-app-gnusocial
@@ -507,7 +507,8 @@ function remove_gnusocial {
         cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 26
         scripts/stopdaemons.sh
     fi
-    kill_pid=$(pgrep "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/queuedaemon.php" | head -n 1)
+    # shellcheck disable=SC2009
+    kill_pid=$(ps aux | grep "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/queuedaemon.php" | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
     kill -9 "$kill_pid"
     pkill "$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/queuedaemon.php"
 
diff --git a/src/freedombone-app-postactiv b/src/freedombone-app-postactiv
index 46db8ad2a..05d025726 100755
--- a/src/freedombone-app-postactiv
+++ b/src/freedombone-app-postactiv
@@ -520,7 +520,8 @@ function remove_postactiv {
         cd "/var/www/$POSTACTIV_DOMAIN_NAME/htdocs" || exit 24
         scripts/stopdaemons.sh
     fi
-    kill_pid=$(pgrep "/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/queuedaemon.php" | head -n 1)
+    # shellcheck disable=SC2009
+    kill_pid=$(ps aux | grep "/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/queuedaemon.php" | head -n 1 | awk -F ' ' '{print $2}')
     kill -9 "$kill_pid"
     pkill "$POSTACTIV_DOMAIN_NAME/htdocs/scripts/queuedaemon.php"
 
diff --git a/src/freedombone-base-email b/src/freedombone-base-email
index afdce281a..675ded441 100755
--- a/src/freedombone-base-email
+++ b/src/freedombone-base-email
@@ -1851,7 +1851,7 @@ function prevent_mail_process_overrun {
     # This prevents any large buildup of exim processes, perhaps due to
     # Tor unavailability, from disabling the server
     { echo '#!/bin/bash';
-      echo "exim_ctr=\$(pgrep \"exim4\" | wc -l)";
+      echo "exim_ctr=\$(ps aux | grep \"exim4\" | grep -v grep | wc -l)";
       echo "if [ \"\$exim_ctr\" -gt 5 ]; then";
       echo '    systemctl stop exim4';
       echo '    exim -bp | exiqgrep -i | xargs exim -Mrm 2> /dev/null';
diff --git a/src/freedombone-ci b/src/freedombone-ci
index 2caa22c37..718290954 100755
--- a/src/freedombone-ci
+++ b/src/freedombone-ci
@@ -117,7 +117,8 @@ if [[ "$1" == 'remove'* ]]; then
         active_builds=$(ps aux | grep "birbci -d" | grep -vc "grep")
         # shellcheck disable=SC2086
         if [ $active_builds -gt 0 ]; then
-            kill_pid=$(pgrep "birbci" | head -n 1)
+            # shellcheck disable=SC2009
+            kill_pid=$(ps aux | grep "birbci" | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
             kill -9 "$kill_pid"
         else
             break
diff --git a/src/freedombone-image-customise b/src/freedombone-image-customise
index 797c274d0..5e6bd84d1 100755
--- a/src/freedombone-image-customise
+++ b/src/freedombone-image-customise
@@ -2165,7 +2165,7 @@ WATCHDOG_SCRIPT_NAME="keepon"
   echo "CURRENT_DATE=\$(date)";
   echo '';
   echo "# keep avahi-daemon daemon running";
-  echo "RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)";
+  echo "RUNNING=\$(ps aux | grep avahi-daemon | grep -v grep > /dev/null && echo Running)";
   echo "if [ ! \$RUNNING ]; then";
   echo "  systemctl start avahi-daemon";
   echo "  echo -n \$CURRENT_DATE >> \$LOGFILE";
diff --git a/src/freedombone-installer b/src/freedombone-installer
index 4f818e477..080183b11 100755
--- a/src/freedombone-installer
+++ b/src/freedombone-installer
@@ -1572,7 +1572,7 @@ function install_apps_from_webadmin {
                     { echo "#!/bin/bash";
                       echo '';
                       echo '# exit if an install is already happening';
-                      echo "kill_pid=$(pgrep \"addremove add\" | head -n 1)";
+                      echo "kill_pid=\$(ps aux | grep \"addremove add\" | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')";
                       echo "if [ \"\$kill_pid\" ]; then";
                       echo "    echo \"App install already in progress\" >> \"$webadmin_install_dir/applog.txt\"";
                       echo "    grep \"addremove add\" | head -n 1 >> \"$webadmin_install_dir/applog.txt\"";
@@ -1729,7 +1729,8 @@ function update_backup_progress_bar {
     fi
 
     backup_process_exists=
-    backup_process=$(pgrep 'webadmin_backup.sh' | grep -v 'grep')
+    # shellcheck disable=SC2009
+    backup_process=$(ps aux | grep 'webadmin_backup.sh' | grep -v grep)
     if [ "$backup_process" ]; then
         if [[ "$backup_process" != *'grep'* ]]; then
             backup_pid=$(echo "$backup_process" | awk -F ' ' '{print $1}')
@@ -1758,7 +1759,8 @@ function update_restore_progress_bar {
         return
     fi
     restore_process_exists=
-    restore_process=$(pgrep 'webadmin_restore.sh' | grep -v 'grep')
+    # shellcheck disable=SC2009
+    restore_process=$(ps aux | grep 'webadmin_restore.sh' | grep -v grep)
     if [ "$restore_process" ]; then
         if [[ "$restore_process" != *'grep'* ]]; then
             restore_pid=$(echo "$restore_process" | awk -F ' ' '{print $1}')
@@ -1787,7 +1789,8 @@ function update_format_progress_bar {
         return
     fi
     format_process_exists=
-    format_process=$(pgrep 'webadmin_format.sh' | grep -v 'grep')
+    # shellcheck disable=SC2009
+    format_process=$(ps aux | grep 'webadmin_format.sh' | grep -v grep)
     if [ "$format_process" ]; then
         if [[ "$format_process" != *'grep'* ]]; then
             format_pid=$(echo "$format_process" | awk -F ' ' '{print $1}')
diff --git a/src/freedombone-mesh-install b/src/freedombone-mesh-install
index fad6cbb55..ef2702b0e 100755
--- a/src/freedombone-mesh-install
+++ b/src/freedombone-mesh-install
@@ -120,7 +120,7 @@ function mesh_avahi {
     WATCHDOG_SCRIPT_NAME="keepon"
     { echo '';
     echo '# keep avahi daemon running';
-    echo "AVAHI_RUNNING=\$(pgrep avahi-daemon > /dev/null && echo Running)";
+    echo "AVAHI_RUNNING=\$(ps aux | grep avahi-daemon | grep -v grep > /dev/null && echo Running)";
     echo "if [ ! \$AVAHI_RUNNING ]; then";
     echo '  systemctl start avahi-daemon';
     echo "  echo -n \$CURRENT_DATE >> \$LOGFILE";
diff --git a/src/freedombone-utils-database b/src/freedombone-utils-database
index c8800a3be..5d63e2e8a 100755
--- a/src/freedombone-utils-database
+++ b/src/freedombone-utils-database
@@ -153,11 +153,14 @@ function get_mariadb_password {
 
 function mariadb_kill_stone_dead {
     systemctl stop mariadb
-    kill_pid=$(pgrep mysqld_safe | head -n 1)
+    # shellcheck disable=SC2009
+    kill_pid=$(ps aux | grep mysqld_safe | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
     kill -9 "$kill_pid"
-    kill_pid=$(pgrep mysqld | head -n 1)
+    # shellcheck disable=SC2009
+    kill_pid=$(ps aux | grep mysqld | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
     kill -9 "$kill_pid"
-    kill_pid=$(pgrep mysqld | head -n 1)
+    # shellcheck disable=SC2009
+    kill_pid=$(ps aux | grep mysqld | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
     kill -9 "$kill_pid"
 }
 
-- 
GitLab