Shell获取进程id

来自linux中国网wiki
跳到导航 跳到搜索

Command

shell脚本中查看某进程ID
比如,查看进程niugge的PID

pid=$(ps -ef | grep jenkins | grep -v grep | awk 'NR==1 {print $2}')



grep niugge, 过滤出niugge所在行
grep -v grep, 过滤掉grep所在行
NR==1,代表第一行
print $2, 返回第二列的值,即PID所在列

最后,返回的值保存到pid中,可以使用此值进程操作。

jepid=$(ps -ef | grep jenkins  | grep -v grep  |awk 'NR==1 {print $2}')
kill $jepid


references

shell获取进程ID