我有几个进程使用相同的名称但具有不同的命令行参数。
$ ps -ef | grep process_name
myusername 19276 6408 0 18:12 pts/22 00.00.00 process_name 4010 127.0.0.1
myusername 23242 6369 0 18:32 pts/11 00.00.00 process_name 4010 127.0.0.2
如何根据进程的完整名称获取进程ID,例如process_name 4010 127.0.0.1
?
我尝试使用pgrep
,但它似乎不会查找参数。
$ pid=$(pgrep process_name)
19276 23242
$ pid=$(pgrep process_name 4010 127.0.0.1) #error
$ pid=$(pgrep "process_name 4010 127.0.0.1") #blank
$ pid=$(pgrep "process_name\s4010\s127.0.0.1") #blank
$ pid=$(pgrep "process_name[[:space:]]4010[[:space:]]127.0.0.1") #blank
-x
代替--exact
以避免插入符号(^)。 - chzzh