#! /bin/bash # process-monitor.sh process=$1 pid=$(ps x | grep "hadoop" | grep '/bin/java' | grep -v grep | awk '{print $1}') echo $pid`
上面是脚本,执行后返回值有多个pid:
10725 10855 11034 11289 11425
比如我想取第一个10725,该怎么写,感谢??。
10725
pid=$(ps x | grep "hadoop" | grep '/bin/java' | grep -m1 -v grep | awk '{print $1}')
还有一种更优雅的解决方案可以避免grep -v grep这种方式:
grep -v grep
pid=$(ps x | grep '[h]adoop' | grep -m1 '/bin/java' | awk '{print $1}')
See: https://unix.stackexchange.co...
2.1m questions
2.1m answers
60 comments
57.0k users