在已知进程名(name)的前提下,交互式 Shell 获取进程 pid 有很多种方法,典型的通过 grep 获取 pid 的方法为(这里添加 -v grep是为了避免匹配到 grep 进程):
ps -ef | grep "name" | grep -v grep | awk '{print $2}'
执行上面的命令就可以获得进程名。
下面shell脚本可以输出脚本 Server 程序的进程id
!/bin/bash
pid=$(ps -ef | grep "Server" | grep -v grep | awk '{print $2}')
echo "get pid $pid"
更多命令可以看这里
https://www.zhangshengrong.com/p/l51gwexJ10/



