How to get child process from parent process

前端 未结 9 744
清歌不尽
清歌不尽 2020-11-30 23:05

Is it possible to get the child process id from parent process id in shell script?

I have a file to execute using shell script, which leads to a new process

9条回答
  •  庸人自扰
    2020-11-30 23:16

    For the case when the process tree of interest has more than 2 levels (e.g. Chromium spawns 4-level deep process tree), pgrep isn't of much use. As others have mentioned above, procfs files contain all the information about processes and one just needs to read them. I built a CLI tool called Procpath which does exactly this. It reads all /proc/N/stat files, represents the contents as a JSON tree and expose it to JSONPath queries.

    To get all descendant process' comma-separated PIDs of a non-root process (for the root it's ..stat.pid) it's:

    $ procpath query -d, "..children[?(@.stat.pid == 24243)]..pid"
    24243,24259,24284,24289,24260,24262,24333,24337,24439,24570,24592,24606,...
    

提交回复
热议问题