How to autostart windows service after install by sc?
Asked Answered
M

2

6

I created a batch file for installing service because I need to install my service on PC don't have Visual Studio.

Content of batch file:

@echo OFF
echo Installing service...
sc create "MyService" binpath= %~dp0\MyService.exe start= auto
echo Installing service complete
pause

And I need to autostart MyService after install, so I make this code:

private void svInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController(svInstaller.ServiceName);
    sc.Start();
}

Don't any problem if I installing my service by Visual Studio Command Prompt with InstallUtil. When I install service by batch file, my service didn't autostart.

How to possible to auto start my service after install by batch file?

Update: Thanks Sam Denty's answer, I problem is resolved.
But I have another question: When I install my service by sc, my code in AfterInstall function do not work?

Medin answered 1/4, 2017 at 19:56 Comment(0)
E
11

This is possible by using either the net start service or sc start command (see previous question on that).

To start a service using sc start, the syntax is:

sc [<ServerName>] start <ServiceName> [<ServiceArguments>]

<ServerName>
    Specifies the name of the remote server on which the service is located. The name must use the Universal Naming Convention (UNC) format (for example, \\myserver). To run SC.exe locally, omit this parameter.
<ServiceName>
    Specifies the service name returned by the getkeyname operation.
<ServiceArguments>
    Specifies the service arguments to pass to the service to be started.

Example:

sc start MyService


Updated script:

@echo OFF
echo Installing service...
sc create "MyService" binpath= %~dp0\MyService.exe start= auto
sc start MyService
echo Installing service complete
pause
Ent answered 1/4, 2017 at 20:3 Comment(1)
That is easy. Maybe, I used the wrong keyword search. But this mean that my code in the afterInstall function do not work when installed with sc?Reshape
I
5

You can install a service in a way that will make it automatically start whenever the Operating System starts.

Services have existence in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services key.

The Start sub key determines how will the service run and when.

The lowest possible values apply to Kernel Drivers:

0 - Boot: Loaded by kernel loader. Components of the driver stack for the boot (startup) volume must be loaded by the kernel loader.

1 - System: Loaded by I/O subsystem. Specifies that the driver is loaded at kernel initialization.

2 - Automatic: Loaded by Service Control Manager. Specifies that the service is loaded or started automatically.

The "auto" options (the "2" value) seems to be your best choice.

Here are the options for calling

SC CREATE

enter image description here So if run the command in your question,

sc create "MyService" binpath= %~dp0\MyService.exe start= auto

since you specify "start = auto", you don't need to do anything else, as the service will automatically autostart.

About the white spaces issue, try this:

SET servicebin=several words.exe
sc create "MyService""%servicebin%" start = auto
Inimical answered 12/8, 2018 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.