使用Python在特定窗口中自动发送CMD命令
我在家庭服务器上运行了几件事,其中两个是Minecraft服务器。我想每周设置一个计算机的自动重新启动,但首先想以正确的方式关闭服务器。这是通过在其特定CMD窗口中发送命令“停止”来完成的。
我已经成功地选择并打开特定的CMD窗口,如下所示:
import pygetwindow as gw
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
我的问题是我似乎无法在这些特定窗口中发送“停止”命令。例如,如果我运行以下命令:
import pygetwindow as gw
import os
print(gw.getAllTitles())
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
os.system("stop")
它不做任何事情,因为它打开了一个新的CMD窗口。可能还有其他问题,但是由于我还没有解决这个问题,所以我还没有遇到过它们。
我真的很感谢您的帮助,因为我被卡住了。
I'm running a couple of things on my home server, two of which are Minecraft servers. I want to set up an automatic restart of the computer every week, but first want to close the servers in the correct way. This is done by sending the command "stop" in their specific CMD windows.
I've successfully been able to select and open specific CMD windows as shown below:
import pygetwindow as gw
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
My problem is that I can't seem to be able to send the "stop" command in those specific windows. If I for example run the following commands instead:
import pygetwindow as gw
import os
print(gw.getAllTitles())
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
os.system("stop")
It doesn't do anything because it opens a new CMD window. There might be other issues as well, but since I haven't been able to solve that issue I haven't encountered them yet.
I would really appreciate some help, cause I'm stuck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用诸如autorestart https://www.spigotmc.org/resources/autorces/autorart。 2538/用于钉子。如果您正在使用香草,我会推荐升级到Spigot(或者我喜欢使用Pufferfish/Paper)
I would use a plugin like AutoRestart https://www.spigotmc.org/resources/autorestart.2538/ for Spigot. If you're on vanilla, I would reccomend upgrading to Spigot (or i like to use Pufferfish/Paper)