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.
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-P
flag forpgrep
shows the children of a certainpid
. 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:25pgrep 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-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:54How 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:02ps -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:10ps -p …
is a way to tell if the process exists. – Kamil Maciorowski Jul 24 '23 at 18:00pgrep bash
relevant to the question? What do we care there are otherbash
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