Sample Header Ad - 728x90

How to use 'pgrep' from a script, when the checked process includes a '-c' (that 'pgrep' considers an argument)?

0 votes
2 answers
394 views
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
Asked by boardrider (262 rep)
Feb 15, 2023, 06:31 PM
Last activity: Mar 30, 2023, 09:39 PM