[英]Get the own process pid from the command prompt in windows
I'm trying to get the PID from command prompt of its own. 我正试图从自己的命令提示符中获取PID。 But when using the below command i'm not getting the desired output always.
但是当使用下面的命令时,我总是得不到所需的输出。 Can you please point out what was the error in the below statement?
你能指出下面的陈述中的错误是什么吗?
cmd.exe /c title="mycmd" & tasklist /v /fo csv | findstr /i "mycmd" & dir & help
Edit: 编辑:
The below command working correctly. 以下命令正常工作。 But when combine it with two more commands.
但是当它与另外两个命令结合时。 It's not working.
它不起作用。
cmd.exe /c title="mycmd" & tasklist /v /fo csv | findstr /i "mycmd"
Additional Info: 附加信息:
I'm using Java 我正在使用Java
final List<String> commands = new ArrayList<String>();
commands.add("cmd.exe");
commands.add("/C");
//.. Add more commands
ProcessBuilder pb = new ProcessBuilder(commands);
You can get the PID of the cmd prompt using below batch file. 您可以使用下面的批处理文件获取cmd提示符的PID。
Reference: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/270f0842-963d-4ed9-b27d-27957628004c/ 参考: http : //social.msdn.microsoft.com/Forums/en-US/msbuild/thread/270f0842-963d-4ed9-b27d-27957628004c/
GetPID.bat GetPID.bat
@echo off
if not defined SESSIONNAME set SESSIONNAME=Console
setlocal
set instance=%DATE% %TIME% %RANDOM%
title %instance%
for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq %instance%" ^| find /i "PID:"`)
do set PID=%%a
if not defined PID for /f "usebackq tokens=2" %%a in (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERNAME%" /FI "WINDOWTITLE eq Administrator: %instance%" ^| find /i "PID:"`)
do set PID=%%a
if not defined PID
echo !Error: Could not determine the Process ID of the current script. Exiting.& exit /b 1
echo PID: "%PID%"
You need to put quotes around the composite command to be executed in the subshell: 您需要在子shell中执行复合命令的引号:
cmd.exe /c "title="mycmd" & tasklist /v /fo csv | findstr /i "mycmd" & dir & help"
Without the quotes, the command is parsed as 如果没有引号,则将命令解析为
(cmd.exe /c title="mycmd") & (tasklist /v /fo /csv) | (findstr /i "mycmd") & (dir) & (help)
so by the time tasklist
runs the instance of cmd.exe
with the specified title has already exited. 所以当
tasklist
运行时,指定标题的cmd.exe
实例已经退出。
Give the CMD window an unique name, in this example "test". 为CMD窗口指定一个唯一的名称,在此示例中为“test”。 The "Windowstitle" is "test*" because the executed command line will be attached to the title.
“Windowstitle”是“test *”,因为执行的命令行将附加到标题。
title test
taskkill /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE ne test*" /f
I found that I had to also use the domain with the user name. 我发现我还必须使用带有用户名的域名。
@IF NOT DEFINED SESSIONNAME (@SET SESSIONNAME=Console)
@SETLOCAL
@SET EXITCODE=0
@SET instance=%DATE% %TIME% %RANDOM%
@TITLE %instance%
@FOR /F "usebackq tokens=1,2" %%a IN (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "WINDOWTITLE eq %instance%" ^| FIND /I "PID:"`) DO @(
@SET PID=%%b
)
@IF NOT DEFINED PID (
@FOR /F "usebackq tokens=1,2" %%a IN (`tasklist /FO list /FI "SESSIONNAME eq %SESSIONNAME%" /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "WINDOWTITLE eq Administrator: %instance%" ^| FIND /I "PID:"`) DO @(
@SET PID=%%b
)
)
@IF NOT DEFINED PID (
@ECHO ERROR: Could not determine the Process ID of the current script.
@SET EXITCODE=1
) ELSE (
@ECHO %PID%
)
@EXIT /B %EXITCODE%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.