linux grep process by name/by id and kill example, and grep -v example
May 28, 2019
Query the name of the running process, usually requires the linux ps command, linux netstat command, linux awk command and the linux grep command.
grep process by name
➜ ps -ef | grep nginx
grep process by port
➜ grep netstat -an | grep "\*\.8080"
grep process id and kill
➜ ps -ef | grep "tail" | grep -v grep | awk -F" " 'system("kill "$2"")'
Command interpretation:
step 1. Show the process you want to kill and exclude the grep command.
➜ ps -ef | grep "tail" | grep -v grep
step 2. Get the process pid you want to kill.
➜ grep ps -ef | grep "tail -f" | grep -v grep | awk -F" " '{print $2}'
step 3. Kill the process with awk system.
➜ ps -ef | grep "tail" | grep -v grep | awk -F" " 'system("kill "$2"")'