If you want to find a process with a given Process ID (PID) you can use PS and GREP
(.. but I'm not showing that command now).
Instead you can use the predefined parameter for that.
The variant using GREP is less good because it will also show any parent or child
process with such an SID, or when it is a "short" number, it may show processes
with that subset of numbers. Even worse it will show commands with that number
anywhere in the process full description, if matching.
So instead, use:
It's quite simple to use loops on files in Bash,
example:
That's it.
I'm using semicolons ( ; ) here because the FOR command is a multi-line command
by design. You can work around this by creating a line feed in the form of the
semicolon character, which normally means some command ended here, and the new
one starts now. But in multi-line commands (like FOR) it behaves as a line feed
(enter).
Rather than using
ps
and
grep
together, you could just use
pgrep
, which is part of the
procps
package (which provides us with the
ps
command and various other tools for getting information from
/proc
).
So, in order to see only information for a parent process that has multiple child-processes open, without seeing the child processes info - I'd do something like this:
Bash:
pgrep nameofprocess | xargs ps -fp
or
Bash:
ps -fp "$(pgrep nameofprocess)"
In the above, we use pgrep to get the pid of the main process and then use ps to get information about it.
For example, if I'm running firefox and I want to see some information about the main process, without seeing the child-processes:
Bash:
ps -fp "$(pgrep firefox)"
And that will show me the following information:
Code:
UID PID PPID C STIME TTY TIME CMD
jason 75543 8403 7 21:28 ? 00:04:25 firefox-esr
And if I want to view information about the main process and all of it's children, I could use
pgrep -f firefox
command to get the PIDs of the main process and it's children and redirect them into
while read
loop to use the
ps -fp
command on each pid.
like this:
Bash:
while read pid; do ps -fp "$pid"; done < <(pgrep -f firefox)
Which will yield information like this:
Code:
UID PID PPID C STIME TTY TIME CMD
jason 75543 8403 8 21:28 ? 00:05:51 firefox-esr
UID PID PPID C STIME TTY TIME CMD
jason 75609 75543 0 21:29 ? 00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -parentBuildID 20231113155436 -prefsLen 40436 -prefMapSize 247635 -appDir /usr/lib/firefox-esr/b
UID PID PPID C STIME TTY TIME CMD
jason 75676 75543 1 21:29 ? 00:00:46 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 2 -isForBrowser -prefsLen 46063 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID PID PPID C STIME TTY TIME CMD
jason 75715 75543 9 21:29 ? 00:06:23 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 3 -isForBrowser -prefsLen 34336 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID PID PPID C STIME TTY TIME CMD
jason 75882 75543 0 21:29 ? 00:00:05 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 7 -isForBrowser -prefsLen 34393 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 20
UID PID PPID C STIME TTY TIME CMD
jason 78563 75543 0 22:06 ? 00:00:02 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 18 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 78696 75543 0 22:09 ? 00:00:09 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 19 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 79139 75543 2 22:15 ? 00:00:31 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 20 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 80365 75543 0 22:27 ? 00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 25 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 80542 75543 0 22:30 ? 00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 26 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 80608 75543 0 22:31 ? 00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 27 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
UID PID PPID C STIME TTY TIME CMD
jason 80730 75543 0 22:33 ? 00:00:00 /usr/lib/firefox-esr/firefox-esr -contentproc -childID 28 -isForBrowser -prefsLen 34429 -prefMapSize 247635 -jsInitLen 240916 -parentBuildID 2
The first PID is the main firefox-esr process.
All of the others are it's children.