知道某具体进程的PID,和程序名(比如 bash,PID 2971),需要将他的子进程都打印出来。
假如所有的bash相关进程如下:
[user@localhost ~]$ ps -ef|grep bash
user 2986 2971 0 21:42 pts/0 00:00:00 bash
user 3368 2971 0 21:59 pts/1 00:00:00 bash
root 15083 2165 0 23:13 ? 00:00:00 /bin/bash /usr/bin/run-parts /etc/cron.monthly
root 15087 15083 0 23:13 ? 00:00:00 /bin/bash /etc/cron.monthly/0anacron
保存文件./subpid
#! /bin/sh input="$@" pids=`ps -ef|grep bash|awk '{if('$input'==$3){print $2}}'` #上面'$input'变量需要比较注意。因为是awk的外部变量,所以不能用双引号。 #如果这里的grep bash替换成grep ${input},得到的结果可能会有所不同。 for pid in ${pids} do echo ${pid} done
在terminal中依次输入:
[user@localhost ~]$ chmod u+x ./subpid
[user@localhost ~]$ ./subpid 2971
2986
3368
另外,参考home_king 大侠的
http://www.linuxsir.org/bbs/showthread.php?s=&threadid=99465
awk引用外部变量的六种方法:
1. awk -v awkvar=$extvar ... #使变量的作用域扩展至BEGIN{}
2. awk '<awk expression>' "awkvar=$extvar" filename #BEGIN段不可用
3. 在awk表达式中使用'$extvar'
4. 模式引用(在正则表达式中引用外部变量) '"$extvar"'
5. 遍历外部数组(用于BEGIN段处理) awk -f 'BEGIN{for(i=1;i<ARGC;i++)print ARGV[i]}' ${extarr[@]}
6. 引用环境变量 ENVIRON["environment variable"]