I have few processes running with same name but different commandline arguments.
$ 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
How can I get the process id based on the full name of the process e.g. process_name 4010 127.0.0.1
?
I tried using pgrep
, but it looks like this does not look into arguments.
$ 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
Use the -f
option to match against full command line:
pgrep -f 'process_name 4010 127.0.0.1'
This will also match subprocess_name 4010 127.0.0.11
. If you want to avoid that, use ^
to anchor the match at the beginning and $
as an anchor at the end:
pgrep -f '^process_name 4010 127.0.0.1$'
From man pgrep
:
-f, --full
The pattern is normally only matched against the process name. When -f is set, the full command line is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With