Given a PPID, how to find all of associated PID? - linux

It might be a weird question as I searched and found many people asking about how to find the PPID given a PID. However, I'm interested in finding out all the PID of a given PPID.
The incentive was to run a process check using pidstat and from my test with pidstat, it seems to be that pidstat only reports meaningful child process stats and if I have a driver script and I want to access each individual processes within the driver script, I need to htop and find out the PIDs individually. When I invoked a driver script, such as bash script.sh, there is a ID associated with this command and this ID becames the PPID of all the processes within the driver scripts (if I understand it correctly).
So does anyone know how to get all the PIDs of a PPID?
Thanks!

Use pgrep. man pgrep for usage.
pgrep -P <ppid>

Try doing this :
$ ps --ppid <YOUR PPID> -o pid=

$ ps -x -o pid,ppid | grep -E '[^0-9]{Enter PPID HERE}$' # on Mac OS Terminal
.
e.g. if ppid = 1
$ ps -x -o pid,ppid | grep -E '[^0-9]1$' # on Mac OS Terminal

Related

How can I know if a process is running background or foreground in commans "ps" or "ps -ef"?

How can I know if a process is running background or foreground in commans "ps" or "ps -ef" ?
I first thought that when I type in ps -ef , there is a section named TTY.
when I look into the list of TTY, most of them are '?' (question marks)
I wonder if that question mark means process running background
I want to check if I am right!
If not, I want to know how I can know if the process is running background or foreground
ps. commnand "jobs" is not used here
I like this question and have a twisted solution for listing ps information about the background jobs/processes.
By definition of a job/background process. They are lists with jobs -l command.
Store the background jobs pid in a list
echo "$(jobs -l)" | awk '{print $2}' > jobs.lst
Intersect the jobs list with current process
ps -eo pid,args | grep -f jobs.lst
The output is ps -eo pid,args result for background jobs.
Providing the id and the command line arguments for the background jobs.
P.S
I used ps output option -o to list only pid and args. Change it to your liking, but make sure pid is there.

identify perf's PID using top/ps

I need to kill perf in a bash script using its PID. However, unable to identify perf's PID (using top). Any pointers appreciated. Ubuntu XX 16.04.2. Thanks.
You can grep ps for the process and get its id from there. then use the output from that with your kill command.
kill $(ps aux | awk '/perf/{print $2}')

Pidof not finding the process

I want to find out a shell script process ID using pidof or ps command or any.
All i want is only the process id of it. I have used 'pidof -x test.sh'. Which is not working. Note: I don't want to invoke the /bin/sh or /bin/bash - because the script will not work. If i invoke /bin/sh in script, pidof is working.
Please help
pgrep -f script is giving the expected result.
Thanks
Another caveat even for non-script processes:
pidof ignores:
zombie processes
processes in disk sleep
So pidof as well as killall (same codebase) unlike pgrep won't not see your process as long as it is blocked in disk i/o.
I just experienced this with pidof - found, not found, found, ...
ps -ef | grep your_search_string | awk {printf $2}
both pidof and pgrep are options to find the pid for a certain process. do not run ps -ef | grep "your_command" because you have now polluted your result with the grep match too.
use pidof -s [program] to find the parent process id.
use pidof [program] to find ALL pids (ie. python is running multiple times on your system).
use pgrep [program] to match the name of your binary that is run by python (because pidof is not well suited for this use-case).
Fox explained the difference between pidof vs pgrep quite well. see his answer
I'll copy it here for convenience:
The programs pgrep and pidof are not quite the same thing, but they
are very similar. For example:
$ pidof 'firefox'
5696
$ pgrep '[i]ref'
5696
$ pidof '[i]ref'
$ printf '%s\n' "$?"
1
As you can see, pidof failed to find a match for [i]ref. This is
because pidof program returns a list of all process IDs associated
with a program called program. On the other hand, pgrep re returns a
list of all process IDs associated with a program whose name matches
the regular expression re.
In their most basic forms, the equivalence is actually:
$ pidof 'program'
$ pgrep '^program$'
As yet another concrete example, consider:
$ ps ax | grep '[w]atch'
12 ? S 0:04 [watchdog/0]
15 ? S 0:04 [watchdog/1]
33 ? S< 0:00 [watchdogd]
18451 pts/5 S+ 0:02 watch -n600 tail log-file
$ pgrep watch
12
15
33
18451
$ pidof watch
18451

How to find a PID of a process whose name I don't know exactly?

I can get the PID of a specific process name by
pidof$(ps -C netns)
but what if I don't know the name of the process exactly?
I can't type something like
pidof$(ps -C net*)
so is there any wildcard character, or is there another solution?
Use the -A (all processes) option and filter the result through grep:
pidof $(ps -A | grep "net*")
Just use pgrep -l, eg:
$ pgrep -l sh
1821 sshd
2590 ssh-agent
2658 sh
2677 bash
3025 gvfsd-trash
14785 ksh93
17723 ksh93
try the following and see if you can discover the process as such
This will give you all processes for all users, in a full-format listing
ps auxf
where :
axu = To see every process on the system using BSD syntax
f = fullformat
if the list is too long you can filter if you have an idea of the process name
For example the command below will show you the pids for chrome.
ps auxf | grep chrome
you can use grep and pip :
pidof$(ps -c |grep yor_pattern)

How to get all of the processes' names of a shell script?

$cat t.sh
grep "12" test | wc -c
I want to know how many processes will be created when it runs.
I used
./t.sh;ps -aux | grep "t.sh"
but it didn't work because "./t.sh" had run over when ps was working.
How can I reach this goal?
Depends on the system you are running on. If you have strace you can trace all the fork system calls. problem is though that somesystems use fork, some vfork and some clone, you will have to experiment. On linux:
strace -c -f -evfork ./t.sh
should give you a summary. -c gives a count, -f means "follow" child processes, and -evfork means trace the vfork kernel call. The output goes to stderr, but you can redirect it to a file using the -o option (there are some neat tricks you can do if you specify a named pipe here).
Your mistake is the semicolon. It tells the shell to wait until t.sh finishes before running grep. Use ampersand instead to tell the shell to run t.sh in the background. Also instead of grep you'd be better off using pstree to see the list of processes spawned by your t.sh script like this:
$ ./t.sh & pstree $(pgrep t.sh)
you can try
./t.sh;ps -aux | grep -E "t.sh"
-E match exactly what you need and nothing else, I'm not on linux right now so I can't be sure, but there's something to do with that option

Resources