Como obtenho a primeira filial PID de uma aplicação no comando pstree?

4

Eu quero obter o PPID do navegador Chromium. Quando eu inicio o chromium-browser , ele lança muitos processos filhos com o mesmo nome.

Por exemplo:

$ 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

Se eu tentar obter PPID de processos filhos como:

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

Imprime o PPID do navegador chromium, mas não é o PID mais alto de chromium-browser de acordo com 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)

Como é possível obter o primeiro PID de ramificação do navegador de cromo ( ├─chromium-browse(6167)─ )?

    
por kenn 10.02.2016 / 11:40

3 respostas

2

Se você estiver procurando pelo processo de cromo de nível superior, pode ser o único com o ID de processo pai 1 (init), tente usar pgrep -P1 chromium para encontrar seu PID.

Você pode tentar usar ps ef para ver os processos listados em uma estrutura de árvore para localizar um pai. Nota: ps switches são ef e não -ef .

    
por 10.02.2016 / 13:22
2

Você poderia tentar com pgrep :

pgrep -o chromium

O sinalizador -o imprimirá apenas o mais antigo (iniciado menos recentemente) dos processos correspondentes. Se todas as suas instâncias de cromo forem processos filho desse processo pai com pid 6167 , esse deve ser o processo chromium-browser mais antigo, portanto, pgrep -o deve imprimir esse pid.

Testado com uma instância do apache:

$ 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
    
por 10.02.2016 / 12:47
1

pgrep é o caminho a percorrer; Eu escrevi isso principalmente como um exercício bash , mas no caso de ser útil para qualquer outra pessoa:

Ele configura uma função que aceita um parâmetro - um nome de processo que, presumivelmente, corresponde a algo na saída ps -eo comm . Ele lê a saída ps , configurando uma matriz de pids e pids pai para processos correspondentes. Feito isso, ele percorre os pids correspondentes para encontrar o que tem um pid pai que não era na lista / matriz original.

# 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
}
    
por 10.02.2016 / 19:02

Tags