Windows service restart

To manually restart a service of Windows, usually we start services.msc (actually mmc.exe ) , then find the service and stop then start it (or use the “restart” directly).

To automate that (for example, batch script), we have several ways :

net utility

This is the ‘old-school’ way

> net stop SERVICE_NAME
> net start SERVICE_NAME

The SERVICE_NAME is the short name of the service, for example the ‘Print Spooler’ service will have the short name: ‘Spooler’ . You can figure it out from services.msc view, or using the below command:

> sc query

To find the service name using the ‘grep-like’ of Unix:

> sc query | find -i "Spooler"

.

sc utility

Service control tool is a bit more powerful than the ‘net’ one. It can query and stop/start service not only on localhost but also other (accessible) remote machines.

> sc \\localhost stop SERVICE_NAME
> sc \\127.0.0.1 start SERVICE_NAME

The net command can also show the ‘display name’ of the services

> net start

.

Windows Management Instrumentation Command-line

The WMIC is very powerful tool for batch script, although it is not used much.

Listing the services (with short name) :

> wmic service list brief

Restart it

> wmic service SERVICE_NAME call StopService
> wmic service SERVICE_NAME call StartService
> wmic /node:localhost service SERVICE_NAME call StartService

.

VBScript

VBS is fine, but I would recommend scripts for Java instead (Apache Ant, for example).

.

Java build with Ant

Some Ant build script like below can do the job as well.

Using utility directly in target:

<target name="stop-Spooler">
    <exec executable="net" vmlauncher="false">
        <arg value="stop"/>
        <arg value="Spooler"/>
    </exec>
</target>
<target name="start-Spooler">
    <exec executable="net" vmlauncher="false">
        <arg value="start"/>
        <arg value="Spooler"/>
    </exec>
</target>

Using macro (and via cmd.exe) :

<project>

    <macrodef name="service">
        <attribute name="service"/>
        <attribute name="action"/>
        <sequential>
            <exec executable="cmd.exe">
                <arg line="/c net @{action} '@{service}'"/>
            </exec>
         </sequential>
    </macrodef>

    <target name="start">
        <service action="start" service="Spooler"/>
    </target>
    <target name="stop">
        <service action="stop" service="Spooler"/>
    </target>

</project>

(Simplified version. The complex one may check OperatingSystem and use ‘sc’ instead of ‘net’ )

 

.

Bonus

Another command to find the short name of services

> reg query HKLM\SYSTEM\CurrentControlSet\Services

 

.

That’s it.

.

./.

About DucQuoc.wordpress.com

A brother, husband and father...
This entry was posted in Coding, Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s