如何在 Windows 上通过 CMD Prompt 发送 Shutdown 命令进行处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6830906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send Shutdown command to process through CMD Prompt on Windows
提问by Senad Me?kin
I'm currently working as software developer and I'm using bunch of tools, so I need to close them all when finished with the work. So what I have done I have created batch file with the tskill command and list of programs that i use e.g.
我目前是一名软件开发人员,我正在使用一堆工具,所以我需要在完成工作后将它们全部关闭。所以我所做的我已经使用 tskill 命令和我使用的程序列表创建了批处理文件,例如
tskill skype
tskill outlook
tskill devenv
tskill explorer
start explorer
There is one problem with this, changes in tools are not saved when process is killed, for exampl last project, opened files, visual positions of controls and all other stuff. These settings are saved only when you close your tool, but it's really time consuming to click each of them.
这有一个问题,当进程被终止时,工具的更改不会被保存,例如最后一个项目、打开的文件、控件的视觉位置和所有其他东西。这些设置仅在您关闭工具时保存,但单击每个设置非常耗时。
So my question is: Can I send shutdown command to software instead killing process?
所以我的问题是:我可以向软件发送关闭命令而不是杀死进程吗?
Note:I need command for CMD, not programing solution, as I have mentioned above I need command like "tskill skype" which kills skype process. Windows can send this command, because when you shutting down your windows, windows is sending shutdown command to all running programs. But if can't be shutdown then windows kill process as I do it above.
注意:我需要 CMD 命令,而不是编程解决方案,正如我上面提到的,我需要像“tskill skype”这样的命令来杀死 Skype 进程。Windows 可以发送此命令,因为当您关闭 Windows 时,Windows 正在向所有正在运行的程序发送关闭命令。但是,如果无法关闭,则 Windows 会像我上面那样杀死进程。
Thanks in advance.
提前致谢。
回答by ddso
Tested on Windows 7:
在 Windows 7 上测试:
taskkill /IM notepad.exe
Note that you have to specify the .exe
extension. I assume this does what you need: It doesn't simply kill the process, but sends the "shutdown" signal, which will display the "Save unsaved changes?" dialog in Notepad and other editors.
请注意,您必须指定.exe
扩展名。我认为这可以满足您的需求:它不会简单地终止进程,而是发送“关闭”信号,该信号将显示“保存未保存的更改?” 记事本和其他编辑器中的对话框。
回答by Julien Roncaglia
If your application support the standard windows messages for it you could use rmtool
to lie to the application and tell it that the computer is shutting down. See How to simulate Windows shutdown for debugging?
如果您的应用程序支持标准的 Windows 消息,您可以rmtool
用来欺骗应用程序并告诉它计算机正在关闭。请参阅如何模拟 Windows 关机进行调试?
To script it you will need to find the PID of the process before (pslist
from sysinternals give the information but you will need to parse the result).
要编写脚本,您需要先找到进程的 PID(pslist
来自 sysinternals 提供信息,但您需要解析结果)。
And if they don't cleanly shutdown just do as windows do when it close : Use rmtool to ask all of them to quit nicely then 30s latter kill everyone still standing (For both outlook and devenv it might even be a little short...).
如果他们没有完全关闭,就像关闭时 Windows 所做的那样:使用 rmtool 要求他们所有人都很好地退出,然后 30 秒杀死仍然站立的每个人(对于 Outlook 和 devenv,它甚至可能有点短...... )。