mySample.sh
pid=$(ps -Af | grep $1 | grep -v grep | awk ' { print $2 } '); echo $pid
The above command is printing and killing the temporary process that was created for grep
Even though i do not have any process running with Abcd,
This is printing pid
Any ways to ignore it,
iam actually ignoring it using grep -v
, still…
./mySample.sh Abcd 6251 6378 6379
Any Issue in fetching the process id.?
Basic command line output is below,After running a process with name Acc_Application_One
[root@localhost Desktop]# ps -Af | grep Acc
root 6251 2758 0 16:16 pts/1 00:00:00 ./Acc_Application_One
root 7288 2758 0 16:57 pts/1 00:00:00 grep Acc
Changed mySample.sh
pgrep -fl "$1"
And the output is
[root@localhost Desktop]# mySample.sh Acc_Application_One
6251 7289
Advertisement
Answer
To kill a process with the pattern anywhere in command line use pkill -f
:
pkill -f "$1"
As per man pkill
:
-f
Match the pattern anywhere in the full argument string of the process instead of just the executable name.
Similarly you can use pgrep -f "$1"
to list the process id of the matching process.