简体   繁体   中英

Desktop shortcut to restart a windows service

Is it possible to create a windows desktop shortcut that will restart a windows service?

I'd like a button to restart my apache service after I have made changes to the config file.

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

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.

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:

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.

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 :

sc <start|stop> <service>

So, eg for starting Apache web server service and MySQL database server service, the batch script named web_servers_start.cmd could look like the following:

sc start "Apache2.2"
sc start MySQL

The batch script must be launched elevated as an administrator. So I created a LNK shortcut which points to the batch script web_servers_start.cmd and checked " Run as administrator " in the file's Properties dialog under the " Advanced... " button on the Shortcut tab .

You can place the LNK shortcut on the desktop, start menu or wherever you prefer.

Note: One of differences between the sc and net commands is that sc sends a message (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM