Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the PID of a process when launching it from command line?

Tags:

process

cmd

pid

Is there a way to do this purely in a .bat file?

The purpose is to launch iexplore.exe, then kill just that instance when it's finished.

like image 452
Nick Avatar asked Nov 27 '09 09:11

Nick


People also ask

How do I find the PID of a running process?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do I find the PID of a process in terminal?

A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do I find my PID in CMD?

Use the Command PromptIn the Start menu search bar, search for command prompt and select Run as administrator. Type tasklist. Press Enter. Command Prompt will now display the PID for the running processes.

How do I display the process ID PID of the current shell?

$ expands to the process ID of the shell. So, you can see the PID of the current shell with echo $$ . See the Special Paramaters section of man bash for more details.


1 Answers

Here's what I use:

@echo off

rem there is a tab in the file at the end of the line below
set tab=    
set cmd=javaw -jar lib\MyProg.jar
set dir=%~dp0

echo Starting MyProg
set pid=notfound

for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
    `wmic process call create "%cmd%"^, "%dir%"`
) do (
    if /I %%i EQU ProcessId (
        set pid=%%j
    )
)

echo %pid% > MyProg.pid

The directory is set to the directory that the cmd file is located in. Change dir to alter that. Modify cmd to change which command is run.

If you want a stop.cmd that kills it, it would look like this

@echo off
for /F %%i in (%~dsp0MyProg.pid) do taskkill /F /PID %%i
del %~dsp0MyProg.pid
like image 156
kybernetikos Avatar answered Sep 28 '22 18:09

kybernetikos