How to Get Pid by Process Name

How to get PID of process by specifying process name and store it in a variable to use further?

If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'

This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh

Windows Ruby get PID from process name

The sys-proctable gem can do that, here is a minimal example for getting the PID of the ruby.exe process on the system:

require 'sys/proctable'

def get_process_id(name)
Sys::ProcTable.ps.detect{|p| p.name == name}&.pid
end

puts get_process_id("ruby.exe")

That doesn't guarantee you will find the parent process tho, you will instead get the process with the lowest PID.

To actually find the "root process", you need to further select processes by checking if their parent process is either non-existent or a different process:

require 'sys/proctable'

def get_parent_process_id(name)
# generate a hash mapping pid -> process info
processes = Sys::ProcTable.ps.map{|p| [p.pid, p]}.to_h

# find the first matching process that has either no parent or a parent
# that doesn't match the process name we're looking for
processes.values.each do |p|
next if p.name != name

if processes[p.ppid].nil? || processes[p.ppid].name != name
return p.pid
end
end

nil
end

puts get_parent_process_id("chrome.exe")

Getting the process id of a process using the process name in perl

Proc::Find::find_proc returns an reference to an Array, not an Array.

So the correct code would be:

 my $pids = find_proc(name => 'my-app-name'); 
foreach my $pid (@$pids) {
print "pid = $pid\n";
}
# or use
# print "pid = ". $pids->[0] . "\n";
# or
# print "pid = $pids->[0]\n";

How to get PID of process by name?

Process has no name, so it is not possible to find PID by searching for some string that may or may not represent a name. What process does have is the command that is used when was fork()ed. So the "name" you see when you use ps is actually argv[0], and as Adam pointed out, you may have hundreds of processes with the same command...

It is possible to give a name to the thread using pthread_setname(). In D set/get the thread name is done using the Thread.name property. Problem is that in most cases developers do not set the thread name...

You can grab a list of running processes by implementing something like readproc (http://procps.cvs.sourceforge.net/viewvc/procps/procps/proc/readproc.c?view=markup) and then sarching for the process you need PID of, but that is not guaranteed to work.

Find Process Name by its Process ID

The basic one, ask tasklist to filter its output and only show the indicated process id information

tasklist /fi "pid eq 4444" 

To only get the process name, the line must be splitted

for /f "delims=," %%a in ('
tasklist /fi "pid eq 4444" /nh /fo:csv
') do echo %%~a

In this case, the list of processes is retrieved without headers (/nh) in csv format (/fo:csv). The commas are used as token delimiters and the first token in the line is the image name

note: In some windows versions (one of them, my case, is the spanish windows xp version), the pid filter in the tasklist does not work. In this case, the filter over the list of processes must be done out of the command

for /f "delims=," %%a in ('
tasklist /fo:csv /nh ^| findstr /b /r /c:"[^,]*,\"4444\","
') do echo %%~a

This will generate the task list and filter it searching for the process id in the second column of the csv output.

edited: alternatively, you can suppose what has been made by the team that translated the OS to spanish. I don't know what can happen in other locales.

tasklist /fi "idp eq 4444" 


Related Topics



Leave a reply



Submit