The following script demonstrates my problem, which is to determine if the sox_user_auditd_v2r -c
process is running:
$ cat ./pgrep_stackexchange_sample.bash
#!/bin/bash -xv
quoted="'$@'"
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -- $quoted > /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
Namely, when I run it like so ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
, pgrep
would misbehave and throw an error:
+ pgrep -x -- '\'\''sox_user_auditd_v2r' '-c\'\'''
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
Namely, pgrep
considered the -c
, which should be part of the regex to be checked, as an argument for pgrep
.
--
Full run output:
$ ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
#!/bin/bash -xv
quoted="'$@'"
- quoted=''''sox_user_auditd_v2r -c''''
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -- $quoted > /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
- pgrep -x -- ''''sox_user_auditd_v2r' '-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...'
Process 'sox_user_auditd_v2r -c' is not running. Starting it...
- sox_user_auditd_v2r -c
- echo 'Process '''sox_user_auditd_v2r' '-c''' started successfully'
Process 'sox_user_auditd_v2r -c' started successfully
- exit 0
Can anyone suggest what would be the correct $quoted
that will allow pgrep
to work as expected?
Edit 1:
Trying to implement @ilkkachu answer doesn't seem to make pgrep
behave. The -c
is still considered as an argument by pgrep
:
$ ./pgrep_stackexchange_sample.bash sox_user_auditd_v2r -c
#!/bin/bash -xv
quoted="$*"
- quoted='sox_user_auditd_v2r -c'
#if pgrep -x -f $@ > /dev/null; then
#if pgrep -x -f $quoted > /dev/null; then
#if pgrep -f $quoted > /dev/null; then
#if pgrep -f -- $quoted > /dev/null; then
#if pgrep -x $quoted > /dev/null; then
if pgrep -x -f $quoted > /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
- 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...'
Process 'sox_user_auditd_v2r -c' is not running. Starting it...
- sox_user_auditd_v2r -c
- echo 'Process '''sox_user_auditd_v2r' '-c''' started successfully'
Process 'sox_user_auditd_v2r -c' started successfully
- exit 0
pgrep -x -- "$@"
work, without your trying to take care of the quoting yourself? (The--
should tellpgrep
already that everything that follows is not to be read as an option.) – DonHolgo Feb 15 '23 at 19:06"$@"
would expand each argument to the script as separate arguments topgrep
, while I think they'd want to join them (pgrep -- sox -c
vs.pgrep -- "sox -c"
). So"$*"
instead. (Passing through the scalar assignmentvar="$@"
forces joining even though that's not what"$@"
is for.) Giving multiple patterns topgrep
seems to be an error in some versions, and in others it looks for processes matching any of the patterns. And looking for just-c
seems odd. – ilkkachu Feb 15 '23 at 19:27Edit 1
in the question ( @DonHolgo, @ilkkachu ) – boardrider Feb 15 '23 at 19:30