【问题标题】:How to list great-grandchildren PIDs of target PID from a Bash script?如何从 Bash 脚本中列出目标 PID 的曾孙 PID?
【发布时间】:2019-09-19 01:11:06
【问题描述】:

我需要编写一个脚本来显示进程的所有曾孙的 PID,这些进程的 PID 作为参数给出。

以下脚本显示进程的孙子进程,其 PID 作为参数给出,但我需要曾孙子进程。

#!/bin/env bash

display_cpid() {
    local depth=$1 pid=$2 child_pid
    (( ++depth ))
    while IFS= read -r child_pid; do
        if (( depth < 2 )); then
            display_cpid "$depth" "$child_pid"
        else
            echo "$child_pid"
        fi
    done < <(pgrep -P "$pid" | xargs)
}

display_cpid 0 "$1"

我希望脚本显示曾孙,但它显示孙。

【问题讨论】:

  • 有什么问题?
  • 我不知道如何让这个脚本显示曾孙。

标签: linux bash shell scripting pid


【解决方案1】:

条件“深度

旁注:脚本无法在 Mint-19 下执行,请考虑修改版本:

#!/bin/bash
display_cpid() {
    local depth=$1 pid=$2 child_pid
    (( ++depth ))
    pgrep -P "$pid" | while read -r child_pid; do
        if (( depth < 3 )); then
            display_cpid "$depth" "$child_pid"
        else
            echo "$child_pid"
        fi
    done 
}

display_cpid 0 "$1"

【讨论】:

    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多