-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.

d-b
  • 1,891
  • 3
  • 18
  • 30
  • 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 Jul 24 '23 at 13:17
  • @aviro I use zsh. The bash process is a shell script that runs in zsh. The bash process then have a python subprocess. There are several identical such bash processes. Yes, I only want to know when a specific such bash process is no longer running. – d-b Jul 24 '23 at 13:20
  • Please [edit] your question and clarify exactly what you need. Again, the -P flag for pgrep shows the children of a certain pid. If they children are not relevant, don't use this flag in your question (since it's confusing), and don't even mention the children (which is misleading). If I understand correctly, neither the parent nor the children are relevant for what you need. If I'm correct, just ask exactly what you need. "Given a pid, how do I know if it's running". The rest is confusing. And if there's a reason you've used -P in the first place, please explain what is the reason. – aviro Jul 24 '23 at 13:25
  • 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 Jul 24 '23 at 13:30
  • @aviro Please read my question again. I want to do something like % pgrep bash -P 47095 and then get the result 47095 # (i.e. no daughter processes). What is unclear about that? – d-b Jul 24 '23 at 13:46
  • Again, -P shows the children processes. If you don't want to see the children, why are you using -P? That's the thing that's not clear. You show a command is doing what it's supposed to do (ie, showing the children), and then you ask how to use this command so it won't show the children? That's why I'm suggesting you [edit] the question to make it clearer. Don't beat around the bush. – aviro Jul 24 '23 at 13:54
  • Here are few examples on how you can ask it: "given a pid, how do I know if it's still running?", or "given a pid, how do I know if it's still running and has a specific name"? or "given a pid I want a command that will return the same pid if the process is still running with a specific name, or an empty output". Those are different questions with different possible answers, and in order to provide the proper answer, we need to know the exact requirement, clear and concise, without misleading and confusing information. – aviro Jul 24 '23 at 13:57
  • @aviro Because in my pgrep's man-page, as far as I can see, P is the only flag that accepts a PID. From the context it is clear that I want to enter a PID and get, basically, true/false back if the process exists or not. – d-b Jul 24 '23 at 14:02
  • And again, your question is How to find one of multiple processes with the same name. So you know how to find multiple process with the same name. You've got a list of those processes. Now you only want to choose one of those? For instance, pgrep bash |head -1 will only show one process named bash out of your list. Is this what you need? If not, you're asking the wrong question. – aviro Jul 24 '23 at 14:02
  • 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.) – Kamil Maciorowski Jul 24 '23 at 14:10
  • @KamilMaciorowski What makes you think I want the exit status when I clearly wrote that I want to know if the process exits or not? – d-b Jul 24 '23 at 17:52
  • @aviro Are you joking or what? I wrote The only thing that differs between them is the pid. I want to write a script [] that only monitors one specific bash process. – d-b Jul 24 '23 at 17:53
  • 1
    "if the process exits" – "Exits" or "exists"? Anyway, the exit status of ps -p … is a way to tell if the process exists. – Kamil Maciorowski Jul 24 '23 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 Jul 24 '23 at 20:34
  • @KamilMaciorowski That was a typo. "ExiSts". – d-b Jul 25 '23 at 18:31
  • @aviro This whole thread of comments is just full someone asking "do you mean A?" to which I reply with a quote from the question that clearly states that I don't mean A. I have posted five comments in that format. Look at your first comment - you clearly think I am running bash as my shell, despite I haven't said a word about that or that my bash process is my shell. Muru below had no problems understanding my un-edited question. You should really consider why (s)he understood and you didn't. – d-b Jul 25 '23 at 18:36
  • Well, I can retort that I understand many other questions, so you should really consider why other authors managed to write a clear question and you didn't. :D – Kamil Maciorowski Jul 25 '23 at 19:46

1 Answers1

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
%
muru
  • 72,889
  • 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 Jul 25 '23 at 18:30