2015年8月17日 星期一

[Linux 常見問題] How to get pid given the process name

Source From Here 
Question 
Hi I have searched various forums and here as well, I could find some answers for Linux and Mac but not able to find solution for Unix and specially Korn Shell.How to get process name (command name) from process id (pid) 

Below reference I found from SO This one And this one also

How-To 
I think it is easier to use pgrep. For example: 
# ps aux | grep java
root 41235 1.0 3.0 2000180 57776 pts/2 Sl 21:35 0:01 java -classpath ...
# pgrep java
41235

Otherwise, you can do it as follows: 
  1. ps -ef | awk '$8=="name_of_process" {print $2}'  
For example: 
# ps -ef | awk '$8=="java" {print $2}'
41235

Better way: Let's check the last column ($NF), no matter its number: 
# ps aux | grep gnome-pty-helper
john 14273 0.0 0.0 8456 216 ? S 05:19 0:00 gnome-pty-helper
root 41334 0.0 0.0 112640 964 pts/2 R+ 21:41 0:00 grep --color=auto gnome-pty-helper

# ps -ef | awk '$NF=="gnome-pty-helper" {print $2}'
14273

If you want to match not exact strings, you can use ~ instead: 
  1. ps -ef | awk '$NF~"gnome-pty-helper" {print $2}'  
Supplement 
Awk Introduction Tutorial – 7 Awk Print Examples

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...