ps: Clean way to only get parent processes?
After discussing with @netcoder on his answer's comments he used a nice trick :D
Using f
on ps
will always get the parent on top which is great.
This should just work:
$ ps hf -opid -C <process> | awk '{ print $1; exit }'
as I mention on the comments, this will return the pid
of just one process.
I would go with:
ps rf -opid,cmd -C <process-name> | awk '$2 !~ /^[|\\]/ { print $1 }'
that is:
- list running processses
r
(ore
if you want everything) - along with parent/children graph
f
- output only the pid and command name
-opid,cmd
- only for the given process
-C <process>
and then
- if the 2nd field - which is the command (
-opid,cmd
) - does not start with a\
or|
then it is a parent process, so print the 1st field - which is the pid.
simple test:
$ ps f -opid,cmd -Cchromium
PID CMD
2800 /usr/lib/chromium/chromium --type=zygote --enable-seccomp-sandbox
2803 \_ /usr/lib/chromium/chromium --type=zygote --enable-seccomp-sandbox
2899 \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/ConnnectB
2906 | \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/Connn
[ ... snip ... ]
2861 \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/ConnnectB
2863 \_ /usr/lib/chromium/chromium --type=renderer --enable-seccomp-sandbox --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/Connn
2794 /usr/lib/chromium/chromium --enable-seccomp-sandbox --memory-model=low --purge-memory-button --disk-cache-dir=/tmp/chromium
2796 \_ /usr/lib/chromium/chromium --enable-seccomp-sandbox --memory-model=low --purge-memory-button --disk-cache-dir=/tmp/chromium
3918 \_ /usr/lib/chromium/chromium --type=gpu-process --channel=2794.45.1891443837 --gpu-vendor-id=0x10de --gpu-device-id=0x0611 --gpu-driver-version -
25308 \_ [chromium] <defunct>
31932 \_ /usr/lib/chromium/chromium --type=plugin --plugin-path=/usr/lib/mozilla/plugins/libflashplayer.so --lang=en-US --channel=2794.1330.1990362572
$ ps f -opid,cmd -Cchromium | awk '$2 !~ /^[|\\]/ { print $1 }'
PID
2800
2794
$ # also supressing the header of ps (top line 'PID') -- add 'h' to ps
$ ps hf -opid,cmd -Cchromium | awk '$2 !~ /^[|\\]/ { print $1 }'
2800
2794
How to get child process from parent process
Just use :
pgrep -P $your_process1_pid
What's the best way to send a signal to all members of a process group?
You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
ps x -o "%p %r %y %x %c "
If it is a process group you want to kill, just use the kill(1)
command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112
.
Get child list of parent process in C
You should keep explicitly all the pids (result of fork(2)...) of your child processes (and remove a pid once you waited it successfully with wait(2) etc...)
It is up to you to choose the data structures to keep these pids.
Any other approach (e.g. using proc(5)... which is what ps
and pstree
are doing.) is not very portable and inefficient.
So the basic rule is that every time you call fork
you should explicitly keep its result (and test for the 3 cases: 0 if in child process, >0 if in parent process, <0 on error) and use that at wait
time.
Read Advanced Linux Programming; it has many pages relevant to that subject.
You might also be interested by process groups and sessions. See setpgrp(2), setsid(2), daemon(3), credentials(7) etc. Notice that with a negative or zero pid kill(2) can send a signal to a process group, and that you could also use killpg(2) for that purpose.
How to get all child process's PIDs when given the parent PID in C
you can use popen
to read the output of the command ps -ef
,then look for the all the child process of a specified PID
int getAllChildProcess(pid_t ppid)
{
char *buff = NULL;
size_t len = 255;
char command[256] = {0};
sprintf(command,"ps -ef|awk '$3==%u {print $2}'",ppid);
FILE *fp = (FILE*)popen(command,"r");
while(getline(&buff,&len,fp) >= 0)
{
printf("%s\n",buff);
}
free(buff);
fclose(fp);
return 0;
}
Related Topics
How to Pass Command Line Parameters from a File
Adding Support for Menuconfig/Kconfig in My Project
Conda.Exe: Error While Loading Shared Libraries: Libz.So.1
Minimizing Copies When Writing Large Data to a Socket
Cross Compiling for Mips Router from X86
Distributing Binary Applications Across Linux Distros
Does The Linux Scheduler Needs to Be Context Switched
Why How to Cast Sockaddr to Sockaddr_In
Linux Bluetooth Programming in C
List Only Duplicate Lines Based on One Column from a Semi-Colon Delimited File
How to Gently Kill Firefox Process on Linux/Os X
Which Linux Distribution Should I Use as a Xen Host
How to Debug a Futex Contention Shown in Strace
Rsync Over Ssh Preserve Ownership Only for Www-Data Owned Files