Sample Header Ad - 728x90

Don't understand why pgrep doesn't work in a bash script with sudo

1 vote
1 answer
1358 views
I'm trying to detect whether the zoom program is running. I also need sudo privileges in my bash script to install it with dpkg. If I use pgrep -f "zoom" >/dev/null 2>&1, the return code echo $? will be correctly set to 0 if zoom is running and to 1 if not found. By using sudo, pgrep picked up two additional processes: the sudo pgrep etc. one and the pgrep etc. child process of the sudo one. This didn't work so I had to use a 'hacky workaround': sudo pgrep -f "[z]oom" >/dev/null 2>&1 did the trick: the regex made sure pgrep processes aren't added to the results. So now with sudo pgrep -f "[z]oom" >/dev/null 2>&1 ; I have echo $? set to 1 if the process is not running and 0 if it's running. Perfect. But when I added this in my bash script (which I run with sudo):
if pgrep -f "[z]oom" 2>&1 ; then
    echo $?
    echo "zoom is running"
This no longer works and it _always_ says that the zoom process is running (so it always returns 0 even if zoom is _not_ running). Why is that?
Asked by Dean (484 rep)
Nov 24, 2021, 04:33 PM
Last activity: Nov 24, 2021, 05:15 PM