How to prevent 'pgrep' to consider the expression it checks as containing arguments for it?
1
vote
1
answer
144
views
I have the following script; it is supposed to start a process if it is not running:
$ cat keepalive_stackexchange_sample.bash
#!/bin/bash -xv
# This script checks if a process is running and starts it if it's not.
# If the process is already running, the script exits with exit code 0.
# If the process was restarted by the script, the script exits with exit code 0.
# If the process fails to start, the script exits with exit code 2.
# If the first argument is "-h" or "--help", the script prints a help message and exits with exit code 10.
#
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "Usage: $0 process_name"
echo "This script checks if a process is running and starts it if it's not."
exit 10
fi
if [[ -z "$1" ]]; then
echo "Error: No process name provided."
echo "Usage: $0 process_name"
exit 12
fi
if pgrep -x -f $@ > /dev/null; then
echo "Process '$@' is already running."
exit 0
else
echo "Process '$@' is not running. Starting it..."
if ! "$@" &> /dev/null; then
echo "Error: Failed to start process '$@'"
exit 2
fi
echo "Process '$@' started successfully"
exit 0
fi
This script works fine if it the process name it gets has only one word, for instance
keepalive_stackexchange_sample.bash sox_authfiles_auditd_v2r
.
However, if the process I'm checking has arguments, pgrep
thinks these arguments are meant for it, and the script does not work as expected, for instance, if I have running:
$ ps -ef | grep [s]ox_ | grep v2r
username 12150 1 0 23:07 ? 00:00:00 sox_user_auditd_v2r -c
$
and I run keepalive_stackexchange_sample.bash sox_user_auditd_v2r -c
, I'll get the following error:
+ pgrep -x -f sox_user_auditd_v2r -c
pgrep: invalid option -- 'c'
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
+ echo 'Process '\''sox_user_auditd_v2r' '-c'\'' is not running. Starting it...'
and the script will run sox_user_auditd_v2r -c
even though it is already running.
Any suggestion how can I have the script work on processes with arguments?
Asked by boardrider
(262 rep)
Feb 14, 2023, 11:23 PM
Last activity: Feb 19, 2023, 03:10 PM
Last activity: Feb 19, 2023, 03:10 PM