linux查看进程printf信息,linux – 查看当前用户进程的祖先和格式化输出

我正在使用ps -eo ppid,pid,cmd查看所有进程,如何将输出格式化为如下所示?

只有给定的PID及其祖先才会打印(直到初始化).

23464 current

|

23211 bash

|

23210 sshd: xxW

|

23193 sshd: WWcccv

|

728 /usr/sbin/sshd –D

|

1 init

如果可能的话,我正在编写一个脚本来使用PID查看祖先进程而不使用pstree.

解决方法:

也许是这样的:

#! /bin/bash -

pid=${1?Please give a pid}

sep=

while

[ "$pid" -gt 0 ] &&

read -r ppid name <

do

printf "$sep%$((3+${#pid}/2))d%$((4-${#pid}/2))s%s\n" "$pid" "" "$name"

sep=$' |\n'

pid=$ppid

done

这使:

$that-script "$$"

13612 /bin/zsh

|

4137 SCREEN screen -xRS main

|

4136 screen screen -xRS main

|

4130 zsh

|

4128 xterm

|

1 /sbin/init splash

这有点迟钝,因为它在祖先的每个进程中运行一个ps.您可以通过运行ps来立即检索所有信息并使用awk对其进行后处理来改进它,例如:

#! /bin/sh -

pid=${1?Please give a pid}

ps -Ao pid= -o ppid= -o args= |

awk -v p="$pid" '

{

pid = $1; ppid[pid] = $2

sub(/([[:space:]]*[[:digit:]]+){2}[[:space:]]*/, "")

args[pid] = $0

}

END {

while (p) {

printf "%s%*d%*s%s\n", sep, 3+length(p)/2, p, 4-length(p)/2, "", args[p]

p = ppid[p]

sep = " |\n"

}

}'

标签:linux,shell,process,ps

来源: https://codeday.me/bug/20190814/1656855.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值