简体   繁体   English

从Windows命令提示符获取自己的进程pid

[英]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实例已经退出。

You are probably missing a few redirection operator. 您可能缺少一些重定向运算符。 Read more here. 在这里阅读更多

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.

相关问题 Java - 在Windows 7中通过命令行获取外部进程的PID - Java - get PID of external process by command line in Windows 7 Java,Windows:获取给定PID的进程名称 - Java, windows: Get process name of given PID Windows批处理文件wmic获取Java进程的PID,如何将标记设置为变量以测试PID是否存在? -如果没有启动,则提示重新启动 - Windows batch-file wmic to get PID of a java process, how do I set tokens to a variable to test if PIDs exist? - start if not,else prompt for restart 从macOS上的open命令启动应用程序进程的pid - Getting the pid from application process launched from open command on macOS 从命令提示符处杀死wine中运行的进程 - kill a process running in wine from command prompt 如何使用 Java 获取在 Windows 中运行的进程的 PID - How to get PID of a process running in Windows using Java 如何在命令行上获取Windows Pid的cpu%使用率? - How can I get the cpu% usage of a Windows Pid on the command line? 批处理文件中的所有过程完成后,关闭Windows命令提示符 - close windows command prompt after all process completed in the batch file 通过Java写入和读取Windows命令提示符 - Write to and read from the Windows Command Prompt by Java 从 Windows 命令提示符中断 JAVA 程序 - Break JAVA program from the Windows command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM