How to get parent pid from a given children pid?
I know I can mannully check it under /proc, I am wonder if there is a smart/better way to achieve this in Ubuntu. Note the parent may or may not be killed.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
ps -o ppid=
ps -o ppid= 2072returns
2061, which you can easily use in a script etc.
ps -o ppid= -C foogives the PPID of process with command
foo. You can also use the old fashioned
ps | grep:
ps -eo ppid,comm | grep '[f]oo'.
ps -f 2072returns
UID PID PPID C STIME TTY STAT TIME CMD izx 2072 2061 0 07:16 ? S 0:00 /usr/lib/pulseaudio/pulse/gconf-helper
pstree -s -p 2072:
if you need the command from this parent pid:
if you need the full command line (with all options):
$PPIDis defined by the shell, it's the PID of the parent process
/proc/, you have some dirs with the PID of each processes. Then, if you
cat /proc/$PPID/comm, you echo the command name of the PID
pstreeby command name
pstree you can search by the child process name and get the Process ID (PID) along with the parents, grandparents and any children of the child process:
$ pstree -hp | grep sleep |-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(8414)
In this case
sleep is the child command and it's PID is
8414. It's parent ID is 841 and is called
display-auto-brightness. The grandparent is a shell (
sh) with a process ID of
839. The great-grandparent is
cron with a process ID of
795. The great-great-grandparent is also
cron with a process ID of
If you want to search by Process ID of
sleep instead of name you can use:
$ pstree -hp | grep 14653 |-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(14653)
sleep process ID changed to
14653. The parent (PID 841) sleeps for 1 minute, wakes up for a split second and then starts a new
sleep command which gets a new process ID. This is another reason why searching for
sleep is easier than searching by process ID.
This code was taken from: Automatically adjust display brightness based on sunrise and sunset and adapted to this question.
To see a nested chain all the way back to boot process use the PID instead of name:
$ pstree -aps 8541 systemd,1 splash fastboot kaslr └─cron,763 -f └─cron,795 -f └─sh,839 -c /usr/local/bin/display-auto-brightness └─display-auto-br,841 /usr/local/bin/display-auto-brightness └─sleep,8541 60
Note: Another minute has passed and the sleep command gets a new PID (8541).
ps -efj can also be used for the same.
> ps -efj | head UID PID PPID PGID SID C STIME TTY TIME CMD root 1 0 1 1 0 Jul01 ? 00:00:13 /sbin/init splash root 2 0 0 0 0 Jul01 ? 00:00:00 [kthreadd] root 3 2 0 0 0 Jul01 ? 00:00:02 [ksoftirqd/0] root 5 2 0 0 0 Jul01 ? 00:00:00 [kworker/0:0H] root 7 2 0 0 0 Jul01 ? 00:06:44 [rcu_sched] root 8 2 0 0 0 Jul01 ? 00:00:00 [rcu_bh] root 9 2 0 0 0 Jul01 ? 00:00:00 [migration/0] root 10 2 0 0 0 Jul01 ? 00:00:08 [watchdog/0] root 11 2 0 0 0 Jul01 ? 00:00:08 [watchdog/1]