4

I want to get PPID of chromium browser. When I launch chromium-browser it launches many child processes with the same name.

For example:

$ pidof  /usr/lib/chromium-browser/chromium-browser
8963 8888 8710 7255 7074 6546 6540 6536 6532 6528 6524 6520 6516 6512 6506 6379 6357 6282 6258 6167 4977 2452

If I try to obtain PPID from child processes like:

$ echo $(ps -o ppid= -p $(pidof -s /usr/lib/chromium-browser/chromium-browser)) 
6282

It prints PPID of chromium browser but it's not the topmost PID of chromium-browser according to pstree:

    ├─chromium-browse(6167)─┬─chromium-browse(6258)───chromium-browse(6282)─┬─chromium-browse(2452)─┬─{chromium-browse}(2460)
    │                       │                                               │                       ├─{chromium-browse}(2461)
    │                       │                                               │                       ├─{chromium-browse}(2467)
    │                       │                                               │                       ├─{chromium-browse}(2527)
    │                       │                                               │                       ├─{chromium-browse}(2574)
    │                       │                                               │                       ├─{chromium-browse}(2918)
    │                       │                                               │                       ├─{chromium-browse}(3711)
    │                       │                                               │                       └─{chromium-browse}(3984)
    │                       │                                               ├─chromium-browse(4977)─┬─{chromium-browse}(4996)
    │                       │                                               │                       ├─{chromium-browse}(5015)
    │                       │                                               │                       ├─{chromium-browse}(5016)
    │                       │                                               │                       ├─{chromium-browse}(5087)
    │                       │                                               │                       ├─{chromium-browse}(5170)
    │                       │                                               │                       ├─{chromium-browse}(5358)
    │                       │                                               │                       ├─{chromium-browse}(831)
    │                       │                                               │                       └─{chromium-browse}(832)
    │                       │                                               ├─chromium-browse(6506)─┬─{chromium-browse}(6509)
    │                       │                                               │                       ├─{chromium-browse}(6607)
    │                       │                                               │                       ├─{chromium-browse}(6616)
    │                       │                                               │                       ├─{chromium-browse}(6636)
    │                       │                                               │                       ├─{chromium-browse}(6754)
    │                       │                                               │                       ├─{chromium-browse}(6849)
    │                       │                                               │                       ├─{chromium-browse}(7045)
    │                       │                                               │                       ├─{chromium-browse}(5651)
    │                       │                                               │                       └─{chromium-browse}(5652)
    │                       │                                               ├─chromium-browse(6512)─┬─{chromium-browse}(6515)
    │                       │                                               │                       ├─{chromium-browse}(6595)
    │                       │                                               │                       ├─{chromium-browse}(6613)
    │                       │                                               │                       ├─{chromium-browse}(6637)
    │                       │                                               │                       └─{chromium-browse}(6753)
    │                       │                                               ├─chromium-browse(6516)─┬─{chromium-browse}(6519)
    │                       │                                               │                       ├─{chromium-browse}(6602)
    │                       │                                               │                       ├─{chromium-browse}(6612)
    │                       │                                               │                       ├─{chromium-browse}(6632)
    │                       │                                               │                       ├─{chromium-browse}(6761)
    │                       │                                               │                       ├─{chromium-browse}(2492)
    │                       │                                               │                       └─{chromium-browse}(6378)
    │                       │                                               ├─chromium-browse(6520)─┬─{chromium-browse}(6522)
    │                       │                                               │                       ├─{chromium-browse}(6600)
    │                       │                                               │                       ├─{chromium-browse}(6623)
    │                       │                                               │                       ├─{chromium-browse}(6629)
    │                       │                                               │                       └─{chromium-browse}(6752)
    │                       │                                               ├─chromium-browse(6524)─┬─{chromium-browse}(6527)
    │                       │                                               │                       ├─{chromium-browse}(6596)
    │                       │                                               │                       ├─{chromium-browse}(6617)
    │                       │                                               │                       ├─{chromium-browse}(6638)
    │                       │                                               │                       └─{chromium-browse}(6766)
    │                       │                                               ├─chromium-browse(6528)─┬─{chromium-browse}(6531)
    │                       │                                               │                       ├─{chromium-browse}(6605)
    │                       │                                               │                       ├─{chromium-browse}(6622)
    │                       │                                               │                       ├─{chromium-browse}(6639)
    │                       │                                               │                       ├─{chromium-browse}(6758)
    │                       │                                               │                       └─{chromium-browse}(6353)
    │                       │                                               ├─chromium-browse(6532)─┬─{chromium-browse}(6534)
    │                       │                                               │                       ├─{chromium-browse}(6598)
    │                       │                                               │                       ├─{chromium-browse}(6618)
    │                       │                                               │                       ├─{chromium-browse}(6642)
    │                       │                                               │                       ├─{chromium-browse}(6756)
    │                       │                                               │                       └─{chromium-browse}(6788)
    │                       │                                               ├─chromium-browse(6536)─┬─{chromium-browse}(6539)
    │                       │                                               │                       ├─{chromium-browse}(6599)
    │                       │                                               │                       ├─{chromium-browse}(6615)
    │                       │                                               │                       ├─{chromium-browse}(6630)
    │                       │                                               │                       ├─{chromium-browse}(6751)
    │                       │                                               │                       ├─{chromium-browse}(6449)
    │                       │                                               │                       └─{chromium-browse}(6450)
    │                       │                                               ├─chromium-browse(6540)─┬─{chromium-browse}(6545)
    │                       │                                               │                       ├─{chromium-browse}(6601)
    │                       │                                               │                       ├─{chromium-browse}(6619)
    │                       │                                               │                       ├─{chromium-browse}(6641)
    │                       │                                               │                       └─{chromium-browse}(6760)
    │                       │                                               ├─chromium-browse(6546)─┬─{chromium-browse}(6549)
    │                       │                                               │                       ├─{chromium-browse}(6606)
    │                       │                                               │                       ├─{chromium-browse}(6614)
    │                       │                                               │                       ├─{chromium-browse}(6631)
    │                       │                                               │                       └─{chromium-browse}(6759)
    │                       │                                               ├─chromium-browse(7074)─┬─{chromium-browse}(7079)
    │                       │                                               │                       ├─{chromium-browse}(7081)
    │                       │                                               │                       ├─{chromium-browse}(7082)
    │                       │                                               │                       ├─{chromium-browse}(7099)
    │                       │                                               │                       ├─{chromium-browse}(7100)
    │                       │                                               │                       ├─{chromium-browse}(7103)
    │                       │                                               │                       └─{chromium-browse}(7109)
    │                       │                                               ├─chromium-browse(7255)─┬─{chromium-browse}(7257)
    │                       │                                               │                       ├─{chromium-browse}(7259)
    │                       │                                               │                       ├─{chromium-browse}(7260)
    │                       │                                               │                       ├─{chromium-browse}(7264)
    │                       │                                               │                       ├─{chromium-browse}(7265)
    │                       │                                               │                       ├─{chromium-browse}(7268)
    │                       │                                               │                       └─{chromium-browse}(7270)
    │                       │                                               ├─chromium-browse(8710)─┬─{chromium-browse}(8713)
    │                       │                                               │                       ├─{chromium-browse}(8714)
    │                       │                                               │                       ├─{chromium-browse}(8715)
    │                       │                                               │                       ├─{chromium-browse}(8718)
    │                       │                                               │                       ├─{chromium-browse}(8719)
    │                       │                                               │                       ├─{chromium-browse}(8850)
    │                       │                                               │                       ├─{chromium-browse}(17470)
    │                       │                                               │                       ├─{chromium-browse}(19250)
    │                       │                                               │                       └─{chromium-browse}(23721)
    │                       │                                               ├─chromium-browse(8888)─┬─{chromium-browse}(8891)
    │                       │                                               │                       ├─{chromium-browse}(8894)
    │                       │                                               │                       ├─{chromium-browse}(8895)
    │                       │                                               │                       ├─{chromium-browse}(8899)
    │                       │                                               │                       ├─{chromium-browse}(8902)
    │                       │                                               │                       ├─{chromium-browse}(8911)
    │                       │                                               │                       ├─{chromium-browse}(9469)
    │                       │                                               │                       └─{chromium-browse}(9471)
    │                       │                                               └─chromium-browse(8963)─┬─{chromium-browse}(8968)
    │                       │                                                                       ├─{chromium-browse}(8969)
    │                       │                                                                       ├─{chromium-browse}(8970)
    │                       │                                                                       ├─{chromium-browse}(8971)
    │                       │                                                                       ├─{chromium-browse}(8977)
    │                       │                                                                       └─{chromium-browse}(8989)
    │                       ├─chromium-browse(6357)─┬─chromium-browse(6379)
    │                       │                       ├─{chromium-browse}(6380)
    │                       │                       ├─{chromium-browse}(6381)
    │                       │                       └─{chromium-browse}(6382)
    │                       ├─{chromium-browse}(6255)
    │                       ├─{chromium-browse}(6283)
    │                       ├─{chromium-browse}(6284)
    │                       ├─{chromium-browse}(6285)
    │                       ├─{chromium-browse}(6288)
    │                       ├─{chromium-browse}(6289)
    │                       ├─{chromium-browse}(6290)
    │                       ├─{chromium-browse}(6291)
    │                       ├─{chromium-browse}(6292)
    │                       ├─{chromium-browse}(6293)
    │                       ├─{chromium-browse}(6310)
    │                       ├─{chromium-browse}(6311)
    │                       ├─{chromium-browse}(6312)
    │                       ├─{chromium-browse}(6313)
    │                       ├─{chromium-browse}(6314)
    │                       ├─{chromium-browse}(6315)
    │                       ├─{chromium-browse}(6316)
    │                       ├─{chromium-browse}(6317)
    │                       ├─{chromium-browse}(6318)
    │                       ├─{chromium-browse}(6319)
    │                       ├─{chromium-browse}(6320)
    │                       ├─{chromium-browse}(6323)
    │                       ├─{chromium-browse}(6351)
    │                       ├─{chromium-browse}(6355)
    │                       ├─{chromium-browse}(6356)
    │                       ├─{chromium-browse}(6375)
    │                       ├─{chromium-browse}(6385)
    │                       ├─{chromium-browse}(6386)
    │                       ├─{chromium-browse}(6455)
    │                       ├─{chromium-browse}(6505)
    │                       ├─{chromium-browse}(6708)
    │                       ├─{chromium-browse}(6781)
    │                       ├─{chromium-browse}(6783)
    │                       ├─{chromium-browse}(7075)
    │                       ├─{chromium-browse}(7076)
    │                       ├─{chromium-browse}(9053)
    │                       └─{chromium-browse}(9089)

How can get the first branch PID of chromium-browser (├─chromium-browse(6167)─)?

  • If you're looking for the top level chromium process, it should be the one with parent process id of 1 (init), try using pgrep -P1 chromium to find its PID – bsd Feb 10 '16 at 12:03
  • @bsd wow you spotted it, it works. Would you post it as an answer? – kenn Feb 10 '16 at 12:19
  • added as an answer – bsd Feb 10 '16 at 12:23
2

If you're looking for the top level chromium process, it could be the one with parent process id of 1 (init), try using pgrep -P1 chromium to find its PID.

You may wish to try using ps ef to see processes listed in a tree like structure to find a parent. Note: ps switches are ef and not -ef.

  • Thank you for the answer. It should also be possible with regex tools such as sed, awk or grep. – kenn Feb 10 '16 at 12:39
  • This will break if your process is started, for example, in a shell. The shell would be the parent process of the browser. – chaos Feb 10 '16 at 13:04
  • 1
    I don't want to annoy you, but $$ gives the process id of the current shell. That does not imply that this is the parent process of the browser; it can be started in another shell. However +1, good answer anyway. =) – chaos Feb 10 '16 at 19:25
  • $BASHPID is no better than $$ (it's the same except in subshells, and except that it works only in bash). – Gilles Feb 10 '16 at 21:42
2

You could try with pgrep:

pgrep -o chromium

The -o flag will only print the oldest (least recently started) of the matching processes. If all your chromium instances are child processes of that parent process with pid 6167, then this must be the oldest chromium-browser process, therefore pgrep -o should print that pid.


Tested with an apache instance:

$ pstree -p
init(1)-+-acpid(1296)
        |-apache2(1312)-+-apache2(29259)
        |               |-apache2(29260)-+-{apache2}(29262)
        |               |                |-{apache2}(29263)
[...]
        |               `-apache2(29354)-+-{apache2}(29356)
        |                                |-{apache2}(29357)
[...]

$ pgrep -o apache2
1312
  • Thank you for responding. I tried it like pgrep -oaf /usr/lib/chromium-browser/chromium-browser but it gives me different PPID 6506 . chromium-browser process PIDs are still actual, they are as it is in posted in question. By the way pgrep -o chromium-browser returns empty string in my distro. I have to use -oaf flags. – kenn Feb 10 '16 at 12:13
  • What is your distro? – chaos Feb 10 '16 at 13:03
  • it's Ubuntu 14.04.3 LTS – kenn Feb 10 '16 at 13:37
  • I'm sorry, it's my fault, pgrep -o chromium-browser returns empty string but pgrep -o chromium works and gives me the exact first branch PPID – kenn Feb 10 '16 at 19:00
  • Ok good, I tought from the example in your question that the process can be matched by the full name. – chaos Feb 10 '16 at 19:31
1

pgrep is the way to go; I wrote this mostly as a bash exercise, but in case it's useful to anyone else:

It sets up a function that accepts one parameter -- a process name that presumably matches something in the ps -eo comm output. It reads the ps output, setting up an array of pids and parent pids for processes that match. Once that's done, it loops through the matched pids to find the one that has a parent pid that was not in the original list/array.

# searches the 'ps' listing for matching ($1) processes,
# then displays the "ancestor" PID -- the process that has a parent PID
# that has a different process name
function ancestor () {
  # must pass a parameter (process name)
  [ -z "$1" ] && return
  local -a array
  local ppid pid comm
  # can't pipe `ps ... | while` as that creates a subshell that loses $array
  while read ppid pid comm
  do
    [ "$comm" = "$1" ] && array[$pid]=$ppid
  done < <(ps -eo ppid,pid,comm)
  # loop through each pid in array and see which value (ppid) is not a member of the array
  for pid in ${!array[@]}
  do
    [ ${array[${array[$pid]}]-unset} = "unset" ] && { echo $pid; break; }
  done
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.