最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

for loop - How can i use %%P inside the filter. I want to close specific cmd.exe windows that match the PID in that list - Stack

programmeradmin3浏览0评论

I have two servers that run in cmd, i want to run stop.bat to close those servers and the windows as well.

If i do taskkill of PID %%P the consoles will not be closed, the servers will be shut down. The problem is when the servers are opened on another account and i try to close the servers the windows will remain opened and not allow me to do changes to the servers location (update the code)

Currently i try to do it this way, but it won't work. It says that the filter can't find anything.


> FOR /F “tokens=* delims=” %%P in (pid_code.txt) DO VAR_SET=%%P taskkill /F /FI “PID eq %VAR_SET%” /IM cmd.exe

The idea was to find a way to add %%P within the quotation marks "" so it can be read in the filter as the values.... this way i will shut the windows where the PID matches. I searched everywhere how to do it but i couldn't find out.

I run this batch file in windows. And its a webbased application in python flask, served by waitress.

EDIT: 01/04/2025 Python code:

pid_file = "path to file"
pid = open(pid_file, "a")
f.write("\n"+str(pid)+"\n")
f.close()

Both servers.py have that line of code in them. This is how i know what PIDs to kill.

Then the stop.bat file has the followinf code:

@echo off
IF EXIST pid_code.txt (
FOR /F "tokens=* delims=" %%P in (pid_code.txt) DO taskkill /F /PID %%P
del "filepath"
) ELSE (
ECHO application isn't running!
TIMEOUT 5
)

The above code kills the PIDs but will leave the server cmd.exe windows open which is a problem if another admin logs in on another account.

I tried replacing FOR line with what was suggested in the comments and it worked.. but there is a but...

setlocal enabledelayedexpansion & FOR /F "tokens=* delims=" %%P in (pid_code.txt) DO SET "VAR=%%P" & taskkill /F /FI "PID eq !VAR!" /IM cmd.exe

Apparently this way the cmd.exes are indeed terminated however the servers still run. And interesting enough cmd.exes have different PIDs than the actual servers which means i cannot identify the correct cmd PID through python.

I tried setting names to the console windows and identify them by the name. However when a different user logs in on the machine, those names are not seen when i run tasklist command, only the PID, this is why i tried to grab the PID.

CURRENT PROBLEM CMD windows PIDs are different than the servers PIDs.

Servers PIDs are in the list and accessible.

There is no way to grab specific cmd.exe pids to close the windows :(

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论