从 pid 获取进程信息(命令等)

从 pid 获取进程信息(命令等)

我有这个代码:

     for job in `jobs -p`; do
         code=0;
         wait ${job} || code=$?
         if [[ "${code}" != "0" ]]; then
            echo "At least one job failed with exit code => ${code}" ;
            exit 1;
         fi
     done

就在该行之后: echo "至少一项作业失败,退出代码 => ${CODE}" ;

我想添加一行来记录失败的命令,如下所示:

     for job in `jobs -p`; do
         code=0;
         wait ${job} || code=$?
         if [[ "${code}" != "0" ]]; then
            echo "At least one job failed with exit code => ${code}" ;
            echo "The job that failed was $(ps -p ${job})"
            exit 1;
         fi
     done

并不是说 ajob只是子进程的 pid。问题是这一行:

echo "The job that failed was $(ps -p ${job})"

并没有什么意义——我需要实际运行的命令,给定 pid=job 。

答案1

当您运行ps命令时,进程已经退出(您正在处理它的返回代码,对吧?)

只需更改逻辑,以便您ps在脚本中较早的位置运行命令并将wait其存储在变量中:

for job in $(jobs -p); do
    process_info=$(ps -p "${job}" -o args=)
    wait "${job}" || {
        code=$?
        echo "Job running [${process_info}] failed with exit code ${code}." >&2
        exit 1
    }
done

答案2

正如所指出的,您不能ps在不存在的进程上使用。

如果您有bash支持关联数组的:

declare -A jobinfo

# collect job information
joblist=( $(jobs -p) )
for pid in "${joblist[@]}"; do
    jobinfo["$pid"]="$( ps -p "$pid" -o args= )"
done

# later...

# wait for jobs
err=0
for pid in "${joblist[@]}"; do
    if ! { wait "$pid" || ! (( code=$? )); }; then
        printf 'Got non-zero exit status (%d) from PID %d: %s\n' \
            "$code" "$pid" "${jobinfo[$pid]}"
        err=1
    fi
done

# exit 0 if all was good, exit 1 otherwise
exit "$err"

在这里,我在开始等待作业退出之前收集了所有必要的数据。我也等全部要完成的作业,以便用户不仅可以获得有关第一个失败作业的信息。


不使用关联数组(应该在 macOS 上默认工作bash):

# collect job information
joblist=( $(jobs -p) )
for pid in "${joblist[@]}"; do
    jobinfo+=( "$( ps -p "$pid" -o args= )" )
done

# later...

err=0
i=0
for pid in "${joblist[@]}"; do
    if ! { wait "$pid" || ! (( code=$? )); }; then
        printf 'Got non-zero exit status (%d) from PID %d: %s\n' \
            "$code" "$pid" "${jobinfo[$i]}"
        err=1
    fi

    i=$(( i + 1 ))
done

exit "$err"

答案3

如果您的操作系统(例如 Linux)有 /proc 目录,您可以从 /proc/$PID 获取所有需要的信息

for PID in $(jobs -p); do
    dosomethingwith /proc/$PID/ # please take a look at what it contents ;-)
    done

相关内容