Create Windows service from executable - windows

Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?

To create a Windows Service from an executable, you can use sc.exe:
sc.exe create <new_service_name> binPath= "<path_to_the_service_executable>"
You must have quotation marks around the actual exe path, and a space after the binPath=.
More information on the sc command can be found in Microsoft KB251192.
Note that it will not work for just any executable: the executable must be a Windows Service (i.e. implement ServiceMain). When registering a non-service executable as a service, you'll get the following error upon trying to start the service:
Error 1053: The service did not respond to the start or control request in a timely fashion.
There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.

Use NSSM( the non-Sucking Service Manager ) to run a .BAT or any .EXE file as a service.
http://nssm.cc/
Step 1: Download NSSM
Step 2: Install your sevice with nssm.exe install [serviceName]
Step 3: This will open a GUI which you will use to locate your executable

Extending (Kevin Tong) answer.
Step 1: Download & Unzip nssm-2.24.zip
Step 2: From command line type:
C:\> nssm.exe install [servicename]
it will open GUI as below (the example is UT2003 server), then simply browse it to: yourapplication.exe
More information on: https://nssm.cc/usage

these extras proved useful.. need to be executed as an Administrator
sc create <service_name> binpath= "<binary_path>"
sc stop <service_name>
sc queryex <service_name>
sc delete <service_name>
If your service name has any spaces, enclose in "quotes".

Many existing answers include human intervention at install time. This can be an error-prone process. If you have many executables wanted to be installed as services, the last thing you want to do is to do them manually at install time.
Towards the above described scenario, I created serman, a command line tool to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run
serman install <path_to_config_file>
will install the service. stdout and stderr are all logged. For more info, take a look at the project website.
A working configuration file is very simple, as demonstrated below. But it also has many useful features such as <env> and <persistent_env> below.
<service>
<id>hello</id>
<name>hello</name>
<description>This service runs the hello application</description>
<executable>node.exe</executable>
<!--
{{dir}} will be expanded to the containing directory of your
config file, which is normally where your executable locates
-->
<arguments>"{{dir}}\hello.js"</arguments>
<logmode>rotate</logmode>
<!-- OPTIONAL FEATURE:
NODE_ENV=production will be an environment variable
available to your application, but not visible outside
of your application
-->
<env name="NODE_ENV" value="production"/>
<!-- OPTIONAL FEATURE:
FOO_SERVICE_PORT=8989 will be persisted as an environment
variable to the system.
-->
<persistent_env name="FOO_SERVICE_PORT" value="8989" />
</service>

Same as Sergii Pozharov's answer, but with a PowerShell cmdlet:
New-Service -Name "MyService" -BinaryPathName "C:\Path\to\myservice.exe"
See New-Service for more customization.
This will only work for executables that already implement the Windows Services API.

I've tested a good product for that: AlwaysUp. Not free but they have a 30 days trial period so you can give it a try...

I created the cross-platform Service Manager software a few years back so that I could start PHP and other scripting languages as system services on Windows, Mac, and Linux OSes:
https://github.com/cubiclesoft/service-manager
Service Manager is a set of precompiled binaries that install and manage a system service on the target OS using nearly identical command-line options (source code also available). Each platform does have subtle differences but the core features are mostly normalized.
If the child process dies, Service Manager automatically restarts it.
Processes that are started with Service Manager should periodically watch for two notification files to handle restart and reload requests but they don't necessarily have to do that. Service Manager will force restart the child process if it doesn't respond in a timely fashion to controlled restart/reload requests.

You can check out my small free utility for service create\edit\delete operations. Here is create example:
Go to Service -> Modify -> Create
Executable file (google drive): [Download]
Source code: [Download]
Blog post: [BlogLink]
Service editor class: WinServiceUtils.cs

Probably all your answers are better, but - just to be complete on the choice of options - I wanted to remind about old, similar method used for years:
SrvAny (installed by InstSrv)
as described here:
https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/create-user-defined-service

I have another method, using the open-source library called Topshelf.
I used it in a c# project, but maybe its available in different programming languages.
Here's a video that explains how to use it a little.
https://www.youtube.com/watch?v=y64L-3HKuP0
The crux of this issue for a lot of people is that you can't install any old .exe as a service unless you use the old method that Tomeg used. I couldn't find the windows nt toolkit that's needed to get that to work.
I was stuck in a corner and this was my way out.

Related

How to I create a Windows service from a script, run as administrator

I have a Windows installer script for a Windows application I'm delivering to customers. I want to have the application installed as a Windows service.
I've been reading up on various ways to do this. The closest I've found that can do this from a command is sc.exe as described in Create windows service from executable here at Stackoverflow, but this command requires running it as administrator, which as far as I can tell, also requires submitting an administrator's password.
In my build script, there's no way to right-click and "run as administrator".
Is there any way to do this from a build script (I'm using my company's installation packager to do this, which uses Ant-like XML build files with an command and statements -- so, much like running at the Windows command line). If I could figure out a command-line implementation that my customers can use I could give them this package.
Thanks for any tips.
Scott

Run a ruby exe file as windows service leads to error

I am stuck on this problem for days, any help will be really appreciated.
Have a ruby file called app.exe which is located in "C:\MyApp\app.exe". This is a windows app which creates a system tray icon. When I click on app.exe directly this works fine. But I would like to make this as a windows service so that it is on the system tray all the time (after system boot to all users).
So I learnt that sc command in windows has some command line arguments which can be used to create a service. I am doing this :
sc create "testservice" binpath= "C:\Prevas\MyApp\app.exe" displayname= "Test Service"
And when I start :
sc start "testservice"
When I do this I get an errror :
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
Not able to understand the reason why I am seeing this error, as I am able to kick start the app directly. Any parameters I am missing or anything I am doing wrong?
Thanks guys
You should have a look at this question.
Check out Win32Utils - Ruby library for MS Windows.
From the doc,
Note that some libraries, such as win32-api and win32-service, have
gems that contain prebuilt binaries so no compiler is necessary for
those libraries. Just install them as you would any other gem.
There are some nice wrappers for these Utils. win32-service is one such gem will help you to acheive that.
Go through these Create a Windows Service with Ruby Part 1 , Part 2 articles by Steve Johnson.

Installing services remotely

Is there a way to install services remotely, without necessarily resorting to .msi packages or full-blown installers? I'm currently using a method similar to the one discussed here:
How to install a windows service programmatically in C#?
to install a service locally, and it works fine. However, I also need to be able to do the same thing remotely. I appreciate any insight.
If you can copy the service binary (the exe file) to the destination computer you can install the service exactly in the same way as you do this locally. The only distinguish is that during the usage of OpenSCManager function (see http://msdn.microsoft.com/en-us/library/ms684323.aspx) you should use destination computer as the first parameter (lpMachineName) and in CreateService (see http://msdn.microsoft.com/en-us/library/ms682450.aspx) as a lpBinaryPathName you should place the path to your service exe how it looks like on the remote computer.
You can use sc.exe utility to do the installation (type" sc create /?" in the command prompt to receive help). The remote installation of the service which you can do with sc.exe you can implement with native Windows API like I short explain above.

Erlang application launch on a Windows server

I have an Erlang application that is deployed on a server with Windows Server 2008.
The way I do this:
Copy application folder in Erlang lib directory.
Open command line (cmd). Execute erl.
Execute application:start(app_name) in Erlang shell.
Are there any better approaches to launch the application? How to make the application to launch on Windows startup?
I have no experience with Windows but...
`1. First of all, you might want to have a look to the concept of release in Erlang. Essentially,
When we have written one or more applications, we might want to create a complete system consisting of these applications and a subset of the Erlang/OTP applications. This is called a release.
`2. Then, you might want to create a script that contains something like:
erl -boot ch_rel-1
Where essentially you're starting Erlang/OTP using a boot script that you created above (just follow the instructions in the releases page)
`3. This article explains how to create startup scripts in Windows Server 2008 (not tested, just googled):
http://technet.microsoft.com/en-us/magazine/dd630947.aspx
Hope this helps. Nice question.
Perhaps rebar might help. It makes building an app skeleton and release quite easy. A nice tutorial is here.
After getting familiar with releases, take a look at manual pages (erl -man ) for start_erl and erlsrv. I used them to start embedded system ( http://www.erlang.org/doc/embedded/embedded_nt.html ) in windows 2003, hope it still works for you in windows 2008.
After creating service with erlsrv it is possible to manage it via standard windows command line and GUI tools, e.g. setting start mode and restart policy.
May be you could start just your application by supplying "-s app_name" as erl/start_erl additional flag, but I didn't try that, as I had to go long route with embedded system release. In that case make sure you have "start() -> application:start(?MODULE)." in your "app_name.erl".

How to make a Win 32 App run as a Windows Service?

Our company just completed a Win 32 Application project, and we have a executable program and it works fine in Windows. Now we have to run it as a windows service under Network account. I am pretty new to Windows Service, so please advice, what should I do to transform this program to a Windows Service? Thank you!
If by "Win32 Application project" you mean "GUI program", then do not run that as a service. Otherwise, here's what you need to do:
Register a service control handler using RegisterServiceCtrlHandler as soon as your program starts. This is required so you can handle the start and stop events. If you do not do this the service manager will kill your process because it thinks it's not a real service.
If you have an installer, you can use the sc command to create the service. For example: sc create MyService binPath= "C:\PathToExe\MyService.exe" type= own start= auto.
Alternatively you can call CreateService if you have code to do this.
If you mean that you want to run your application as a windows service, there is a utility in Windows Resource Kits. It's name is srvany.exe and you can find documentation about it here:
http://search.microsoft.com/search/results.aspx?st=b&View=en-us&s=1&c=0&qu=137890
Srvany.exe can be downloaded as part of Windows Server 2003 Resource Kit Tools here:
http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
Srvany.exe is a fine, free solution (as described by alemjerus), but please check out AlwaysUp if you are working in a professional environment and need a robust/advanced feature set.
Good luck!

Resources