2

I want to build a small script (called check_process.sh) that checks if a certain process $PROC_NAME is running. If it does, it returns its PID or -1 otherwise.

My idea is to use pgrep -f <STRING> in a command substitution.

If I run this code directly in the command line:

export ARG1=foo_name
export RES=$(pgrep -f ${ARG1})
if [[ $RES == "" ]]; then echo "-1" ; else echo "$RES"; fi

everything goes fine: PID or -1 depending on the process status.

My script check_process.sh contains the same lines plus an extra variable to pass the process' name :

#!/bin/bash
export ARG1=$1
export RES=$(pgrep -f ${ARG1})
if [[ $RES == "" ]]; then echo "-1" ; else echo "$RES"; fi

But this code does not work! If the process is currently running I get two PIDs (the process' PID and something else...), whereas when I check a process that is not running I get the something else !

I am puzzled. Any idea? Thanks in advance!

eddie
  • 1,252
  • 3
  • 15
  • 20

1 Answers1

2

If you add the -a flag to pgrep inside your script, you can see something like that (I ran ./check_process.sh vlc):

17295 /usr/bin/vlc --started-from-file ~/test.mkv
18252 /bin/bash ./check_process.sh vlc

So the "something else" is the pid of the running script itself.

The pgrep manual explains the -f flag:

The pattern is normally only matched against the process name. When -f is set, the full command line is used.

Obviously, the script command line contain the lookup process name ('vlc') as an argument, hence it appears at the pgrep -f result.

If you're looking just for the process name matches you can remove the -f flag and get your desired result.

If you wish to stay with the -f flag, you can filter out the current pid:

#!/bin/bash
ARG1=$1
TMP=$(pgrep -f ${ARG1})
RES=$(echo "${TMP}" | grep -v $$)
if [[ $RES == "" ]]; then echo "-1" ; else echo "${RES}"; fi
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • If I remove the `-f` the script is not working anymore. I haven't written in the question, but actually my process name is a path.. the process I am looking for is a script and "this path" is one of its parameters. – eddie Aug 17 '16 at 10:48
  • Thanks for your code! I was trying do the same with `ps`! – eddie Aug 17 '16 at 11:41
  • I corrected slightly your code: on line 4 `${RES}` had to be `${TMP}` – eddie Aug 17 '16 at 12:04
  • I accepted you answer but I have an extra question. Ca you clarify what did you mean with "If you add the `-a` flag in your script" ? – eddie Aug 17 '16 at 12:11
  • Sure. I meant if you use `RES=$(pgrep -a -f ${ARG1})` in your original script – Ohad Eytan Aug 17 '16 at 12:54
  • Which OS are you using? On my Linux box I get `pgrep: invalid option -- 'a'` – eddie Aug 17 '16 at 13:24
  • linux with `pgrep 3.3.12` – Ohad Eytan Aug 17 '16 at 13:26
  • okay maybe it is something new. I have `pgrep -V : pgrep (procps version 3.2.7)` – eddie Aug 17 '16 at 13:33
  • Apparently. You can see the relevant manual [here](http://www.polarhome.com/service/man/?qf=pgrep&af=0&sf=0&of=Archlinux&tf=2) – Ohad Eytan Aug 17 '16 at 13:34