gpt4 book ai didi

java - 如何检查批处理文件是否正在 Windows 上运行?

转载 作者:行者123 更新时间:2023-12-02 06:10:57 25 4
gpt4 key购买 nike

我正在 Windows 中运行一个进程。该过程由批处理文件“skrmedpostctl_start.bat”启动。我正在编写一个使用 skrmedpostctl 输出的 java 项目。我的系统可以在Linux平台上运行,但不能在Windows平台上运行。问题是,在执行这个 skrmedpostctl (Linux 中的 shell 脚本,Windows 中的批处理文件)之前,我会检查它是否已经在运行。这是我到目前为止所拥有的:

switch (CURRENT_OS) {
case LINUX:
String[] procCheck_SKR = new String[]{"/bin/bash", "-c", "ps -ef | grep MedPost-SKR"};
String[] procCheck_WSD = new String[]{"/bin/bash", "-c", "ps -ef | grep WSD_Server"};
try {
String procs_SKR = systemCall(procCheck_SKR);
String procs_WSD = systemCall(procCheck_WSD);
if (procs_SKR.split("\n").length < 2)
systemCall(new String[]{"/bin/bash", "-c", MM_BIN_DIR + "./skrmedpostctl start"});
if (procs_WSD.split("\n").length < 2)
systemCall(new String[]{"/bin/bash", "-c", MM_BIN_DIR + "./wsdserverctl start"});
} catch (IOException e) { e.printStackTrace(); System.exit(0); }
break;
case WINDOWS:
String[] procCheck = new String[]{"cmd.exe", "/c", System.getenv("windir") +"\\system32\\tasklist.exe"};
String pidInfo = "";
try {
Process proc = Runtime.getRuntime().exec(procCheck);
BufferedReader reader = new BufferedReader(new InputStreamReader( proc.getInputStream() ) );
for (String line; (line = reader.readLine()) != null; )
pidInfo += line;
reader.close();
} catch (IOException e) { e.printStackTrace(); System.exit(0); }
if (pidInfo.contains("NAME OF MY PROCESS")) {
// do stuff
}
break;
default: throw new IllegalArgumentException("Can't run this program on " + CURRENT_OS);
}

问题是,当我运行批处理文件时,任务管理器仅显示“Windows 命令处理器”,而不是实际名称。因此,虽然在 Linux 中我可以执行“ps -ef | grep ...”来检查它是否已经在运行,但我似乎无法在 Windows 中执行相同的操作。有没有办法做到这一点?任何帮助将不胜感激。

最佳答案

tasklist /v

在详细模式 (/v) 下,tasklist 返回窗口的标题。您可以测试批处理文件的名称或批处理文件内部设置的特殊标题。

关于java - 如何检查批处理文件是否正在 Windows 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com