-2

I have several bash scripts running. The only thing that differs between them is the pid. I want to write a script like this that only monitors one specific bash process and exclude any daughter processes. I have seen this question but in that case the process names differs and you just have to write a precise regular expression.

Currently, if I do

% pgrep bash 
40583
47095
48133
49244

and if I do

% pgrep -P 47095
47099
50151

I want to do something like

% pgrep bash -P 47095

and then get the result

47095 # (i.e. no daughter processes)

or an empty return value if 47095 no longer exists. How can this be achieved? The answer doesn't need to be based on pgrep, what is important is that it only returns one line if the process is running and nothing if there is no such process.

17
  • I don't understand the question. First you can a list of process named bash. Then you check the children of one of those processes using the -P flag (a flag that shows the children of a specific pid). But then you say you don't want to see the children. If you don't want to see the children, why do you use the -P flag? Can you provide an example including the process names? Do you only want to know that given a pid, how to know if this pid is still running?
    – aviro
    Commented Jul 24, 2023 at 13:17
  • 1
    Also, when you run pgrep bash, you get a list of pids. Do you only want to choose one of those randomly? Again, your question is not clear, and would require some example.
    – aviro
    Commented Jul 24, 2023 at 13:30
  • 3
    "I want to enter a PID and get, basically, true/false back if the process exists or not" – So maybe you want the exit status of ps -p …. If so, the question is very confusing indeed. (If not, it's also confusing, because it's still not clear what you want.) Commented Jul 24, 2023 at 14:10
  • 1
    "if the process exits" – "Exits" or "exists"? Anyway, the exit status of ps -p … is a way to tell if the process exists. Commented Jul 24, 2023 at 18:00
  • 1
    Look, you can continue to argue. The only thing that matters is that the people who read this question don't understand it. If more than one person says that, maybe they're not wrong. You can take it and try to rephrase your Q, or ignore us and get no answer. There are still so many unclear things. You only want to monitor one pid. Which one? One that you choose? In that case, how is the first pgrep bash relevant to the question? What do we care there are other bash processes? How does it contribute to the question and the people that read it? Again, confusing and misleading.
    – aviro
    Commented Jul 24, 2023 at 20:34

1 Answer 1

1

I think what you might want is the PID file processing of pgrep, instead of using -P:

-F, --pidfile file
   Read PIDs from file.  This option is more useful for pkill or pidwait than pgrep.

In action:

% sleep 100 &
[1] 26819
% echo 26819 > pid
% pgrep -F pid sleep
26819
% pgrep -F pid sleep -l
26819 sleep
% pgrep -F pid awake -l
%
1
  • I saw that but I don't have a PID-file. I can of course create one, but it feels backward to create a file instead of just enter the value on the command line. Any ideas why they decided to do it like this? Besides, does this solution ignore child processes?
    – d-b
    Commented Jul 25, 2023 at 18:30

Not the answer you're looking for? Browse other questions tagged .