用于重新启动 Windows 服务的桌面快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1995847/
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
Desktop shortcut to restart a windows service
提问by Jon Winstanley
Is it possible to create a windows desktop shortcut that will restart a windows service?
是否可以创建一个 Windows 桌面快捷方式来重新启动 Windows 服务?
I'd like a button to restart my apache service after I have made changes to the config file.
在我更改配置文件后,我想要一个按钮来重新启动我的 apache 服务。
回答by Daniel A. White
You can do this in a batch file, then make a shortcut to it.
您可以在批处理文件中执行此操作,然后为其创建快捷方式。
Create a text file with the following content, but save it with the file extension .bat
创建具有以下内容的文本文件,但使用文件扩展名 .bat 保存它
net stop "Service Name"
net start "Service Name"
Once the file exists, you can create a shortcut to it, and even assign a keyboard shortcut too if deemed necessary.
文件存在后,您可以为其创建快捷方式,甚至可以根据需要分配键盘快捷方式。
回答by Jason
You can accomplish this without a batch file using the following shortcut target:
您可以使用以下快捷方式目标在没有批处理文件的情况下完成此操作:
C:\Windows\System32\cmd.exe /c "net stop "Service Name" & net start "Service Name""
In addition to the answer the following comment by Tibo is mandatory:
除了答案之外,Tibo 的以下评论是强制性的:
To make it run as Administrator (required for some services, maybe all?), in the shortcut's properties window, tab Shortcut, clic on the button "Advanced..." (at the bottom) then check "Run as administrator". It will open the User Account Control popup each time.
要使其以管理员身份运行(某些服务需要,也许所有服务都需要?),在快捷方式的属性窗口中,选择“快捷方式”,单击“高级...”按钮(底部),然后选中“以管理员身份运行”。它每次都会打开用户帐户控制弹出窗口。
回答by David Ferenczy Rogo?an
I'm using a system consisting of a simple CMD batch script and an LNK shortcut. The batch script contains the sc
command, which acts as a Windows services controller. For starting or stopping a service it uses the same parameters as the net
command:
我使用的系统由一个简单的 CMD 批处理脚本和一个 LNK 快捷方式组成。批处理脚本包含sc
命令,它充当 Windows 服务控制器。对于启动或停止服务,它使用与net
命令相同的参数:
sc <start|stop> <service>
sc <start|stop> <service>
So, e.g. for starting Apache web server service and MySQL database server service, the batch script named web_servers_start.cmd
could look like the following:
因此,例如,为了启动 Apache Web 服务器服务和 MySQL 数据库服务器服务,名为的批处理脚本web_servers_start.cmd
可能如下所示:
sc start "Apache2.2"
sc start MySQL
The batch script must be launched elevated as an administrator. So I created a LNK shortcutwhich points to the batch script web_servers_start.cmd
and checked "Run as administrator" in the file's Propertiesdialog under the "Advanced..." button on the Shortcut tab.
批处理脚本必须以管理员身份启动。因此,我创建了一个指向批处理脚本的LNK 快捷方式,web_servers_start.cmd
并在“快捷方式”选项卡上的“高级...”按钮下的文件属性对话框中选中了“以管理员身份运行” 。
You can place the LNK shortcut on the desktop, start menu or wherever you prefer.
您可以将 LNK 快捷方式放置在桌面、开始菜单或您喜欢的任何位置。
Note:One of differences between the sc
and net
commands is that sc
sends a message (e.g. start
) to a service and ends itself immediately while net
waits until service operation is done. If you don't need to manipulate with operation status or error code, sc
command is much faster.
注意:sc
和net
命令之间的区别之一是sc
向start
服务发送消息(例如)并在net
等待服务操作完成时立即结束自身。如果不需要用操作状态或错误代码进行操作,sc
命令会更快。