3

I have portable version of an app. I have ran for example 5 multiple instances of it and they all have the same process name but different pids. I want to find a way to for example kill one specific process, cause using taskkill with the name of that process would kill all of those. I want to kill just the specific one by providing the pid of that process.
Now the question is: How can I find a process pid so that I can use this number to kill that specific application easily?
For example I want to kill the third one (I mean by using time).
Can pid gives me information of when a process was ran? If not What are the other workarounds?

7
  • That is not possible under windows (and i don't think its possible under linux and unix either), but you can get and kill windows filtering by process name, main window handler, window class and some other methods.
    – Felype
    Apr 25, 2015 at 21:14
  • 1
    You are asking the wrong question. Since the instances already have different PIDs (as you've yourself stated), there's no need to change these PIDs when you just want to kill certain instances. See my answer below.
    – Karan
    Apr 25, 2015 at 21:18
  • @Karan Lets make this interesting. Assume I want to do this from a batch file (automate things and schedule them) and I dont know what is the pid of that process (and again there are multiple instances with same names).
    – user441228
    Apr 26, 2015 at 21:26
  • 2
    Perhaps the question you should be asking is "When I start an application (e.g., from a batch file), how can the batch script learn the PID of the application process, so it can use it later to kill the process?"  (The answer might be: run tasklist with output to a file before and after starting the application, then compare the output files to see what PID is in the second file but not the first.)  Or perhaps, "Given multiple Windows processes, how can I distinguish among them (e.g., by start time or other discriminators)?" Apr 27, 2015 at 4:11
  • Also, (1) You might want to tag your question with [windows] (ideally, specifying the exact version that you're using) and [batch], because, until you edited the question, there was no way to tell that you weren't talking about Unix.  Also, (2) I don't understand what you mean by "I have an application installed on my computer ... and I have the portable version of that too."  If that's important to your question, you might want to reword it. Apr 27, 2015 at 4:12

5 Answers 5

3

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)
10
  • I'm not the one to downvote, but that answer seems a bit off the question. The question was: "How to change the PID of a process".
    – Felype
    Apr 25, 2015 at 21:12
  • @Felype: Read my answer. It's an X-Y question. The OP is trying to kill using the process name, but doesn't need to since taskkill can already kill using specific PIDs. What he wants to do (change PIDs) is useless for the purpose of killing instances since they already have different PIDs by his own admission.
    – Karan
    Apr 25, 2015 at 21:14
  • Yes I understand, present him the other filters, he clearly stated to know how to kill by PID, present now the /f method to filter by imagename, window name, etc. Again, I didn't downvote.
    – Felype
    Apr 25, 2015 at 21:15
  • @Felype: I'm not accusing you of being the downvoter. All I'm saying is that clearly the OP's on the wrong track and my answer is relevant, so the downvote is IMO stupid unless the voter can give me a good reason for it.
    – Karan
    Apr 25, 2015 at 21:16
  • Indeed. I just said what I think it might be. The OP did mention he knows how to taskkill /pid, so this answer doesn't answer at all, even if the question may sound stupid.
    – Felype
    Apr 25, 2015 at 21:18
4

You can do this with powershell. For example, this lists all the processes named "chrome" on my machine and includes their starttime, sorted by their start time.

PS C:\Users\Zach> Get-Process chrome | Select-Object name,id,starttime

Name       Id StartTime
----       -- ---------
chrome   1752 4/16/2015 5:53:05 PM
chrome   5404 4/16/2015 5:53:05 PM
chrome   3980 4/16/2015 5:53:05 PM
chrome   4784 4/16/2015 5:53:05 PM
chrome   4336 4/16/2015 5:53:05 PM
chrome   4492 4/16/2015 5:53:05 PM
chrome   8812 4/16/2015 5:58:19 PM
chrome   9908 4/16/2015 5:58:19 PM
chrome   3608 4/16/2015 5:58:19 PM
chrome   9980 4/16/2015 5:58:19 PM
chrome   8536 4/16/2015 5:58:19 PM
chrome   9664 4/16/2015 5:58:19 PM
chrome   1700 4/16/2015 6:02:26 PM
chrome   9712 4/16/2015 7:50:31 PM
chrome   5920 4/16/2015 7:50:31 PM
chrome   4572 4/16/2015 7:50:31 PM
chrome   2400 4/17/2015 6:19:58 AM
chrome  11780 4/21/2015 7:17:22 PM
chrome  11340 4/21/2015 8:19:17 PM
chrome   7828 4/21/2015 8:19:19 PM
chrome   9448 4/21/2015 8:32:05 PM
chrome   3400 4/22/2015 7:48:21 PM
chrome   8860 4/22/2015 7:53:28 PM
chrome  10364 4/24/2015 1:04:54 AM
chrome   4596 4/24/2015 9:52:24 AM
chrome  13392 4/24/2015 10:39:31 AM
chrome  14596 4/24/2015 10:44:28 AM
chrome   3252 4/24/2015 10:49:28 AM
chrome  16100 4/24/2015 11:31:08 AM
chrome  13840 4/24/2015 3:14:34 PM
chrome   4472 4/24/2015 6:31:11 PM
chrome  13652 4/24/2015 6:31:31 PM
chrome  12008 4/25/2015 12:51:15 AM
chrome  16016 4/25/2015 12:51:17 AM
chrome   9852 4/25/2015 1:49:19 PM
chrome  14548 4/25/2015 2:10:19 PM
chrome  16364 4/25/2015 9:18:12 PM
chrome  13860 4/26/2015 12:21:57 AM
chrome  13004 4/26/2015 10:07:53 AM
chrome   1364 4/26/2015 10:13:30 AM
chrome  12464 4/26/2015 10:13:38 AM
chrome  15144 4/26/2015 10:13:52 AM
chrome   1040 4/26/2015 10:28:49 AM
chrome  15800 4/26/2015 10:38:00 AM
chrome  12984 4/26/2015 2:51:06 PM
chrome  12972 4/26/2015 7:18:05 PM
chrome  11816 4/26/2015 7:24:32 PM
chrome  15044 4/27/2015 6:54:53 PM
chrome  11916 4/27/2015 8:00:45 PM
chrome  16216 4/28/2015 12:45:07 AM
chrome  11404 4/28/2015 8:09:43 PM
chrome  15680 4/28/2015 8:57:05 PM
chrome   6864 4/28/2015 9:12:56 PM
chrome  16172 4/28/2015 11:34:43 PM
chrome  10432 4/28/2015 11:56:50 PM
chrome  14856 4/29/2015 12:43:49 AM
chrome  17372 4/29/2015 12:45:57 AM

Using this, you can find the pid of the one you want to kill. For example:

PS C:\Users\Zach> Stop-Process 3980

to stop the 3rd one in the list.

2

If you want a quick way to find the PID of a program using only the built-in tools, you can do all this with Windows Task Manager.

  • On the Applications tab and find the application name. If you aren't sure which one is the correct one, then right click it and choose Bring To Front. If the right one comes to the front, then you have the correct entry.
  • Right click the application name and choose Go to Process.
  • Optionally, if you want the PID displayed, go to View --> Columns and make sure that PID (Process Identifier) is selected.
  • Right click the process that is highlighted, and choose End process.
0

In Windows (I assume from "taskkill" that you're talking about Windows), the process ID is picked by the operating system. It is not something you can "set".

It is not predictable, either.

Sorry about that.

2
  • This seems more like a comment. There is an answer to this question, it might not involve the approach the author wants, but there is an answer to this question.
    – Ramhound
    Apr 25, 2015 at 21:34
  • Beg pardon, but the explicitly stated question was: "Now the question is [note wording]: How can I set a process pid?" The answer is : You can't. Apr 25, 2015 at 23:52
0

The PID of a process is an unique identifier. 2 or more instances cannot have the same PID and the PID is managed internally by the Operating System Kernell, it cannot be changed at all.

As far as I understand this question, you mentioned taskkill I'm assuming this refers to Windows's taskkill command.

If you need to kill processes by Image name, use /f /imcommand line argument:

Eg:

taskkill /f /im notepad.exe

This will kill all instances of notepad.exe

if you need a PID filter to kill process in an interval, use /f /fi

taskkill /f /fi "PID gt 1000"

This kills all processes with PID above 1000

if you need to kill all process from a specific user

taskkill /f /fi "USERNAME eq Batman"

This kills all processes by user Batman

if you need to kill all process from a specific user on a remote machine with RPC service enabled

taskkill /s GothamServer /f /fi "USERNAME eq Batman"

This kills all processes by user Batman on remote machine GothamServer

For more information on filters, check taskkill /?

2
  • This kills all processes with PID above 1000 What do you mean by that? Do pid numbers go up step by step when a new program is ran?
    – user441228
    Apr 26, 2015 at 21:34
  • 1
    not really, The Operating System defines PID in a very 'magical' way, not sequential and sometimes a program opened later will have a PID with lower value than the program opened earlier. That is just a "form of filtering" you can filter PIDs between X and Y, but I think that won't fit for your needs, check /f /immethod instead.
    – Felype
    Apr 27, 2015 at 1:31

You must log in to answer this question.