How to start a CLI application as a Windows service?

Let's say I have a third-party application that does background work, but displays all errors and messages to the console. This means that at present, we must support user login to the server and restart the application (double-click) each time we restart.

Not very cool.

I was sure that there was an easy way to do this - a common shell common service that could be configured with a log file for stdout and stderr .

I checked svchost.exe , but according to this site , it is only for dll files. A pity.

EDIT: The application must be run from a batch file. FireDaemon seems to be doing the trick, but I think it's a bit overkill, for something that can be done in & 10 lines of python code ... Oh, well, not invented here ...

+6
windows windows-services
source share
5 answers

Browse srvany.exe from the Resource Kit . This will allow you to run something as a service.

You can pass the parameters in the service definition to your executable using srvany.exe so that you can run the batch file as a service by setting the registry as follows:

 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Parameters] "Application"="C:\\Windows\\System32\\cmd.exe" "AppParameters"="/CC:\\My\\Batch\\Script.cmd" "AppDirectory"="C:\\My\\Batch" 

Note: if you install these keys in RegEdit , rather than using a file, you only need one-time backslashes in the values.

+10
source share

I would recommend NSSM: Non-sucking service manager .

  • 32/64-bit EXE
  • Public Domain (!)
  • Correctly implements service stop messages and sends the right signal to your applications for a graceful shutdown.
+4
source share

Why not just implement a very thin shell of the service, here is a quick start guide for writing a service in .NET. Writing a Useful Windows Service in .NET in Five Protocol

When you run this, you can use the Process class to start the application and configure it so that you can handle stdout / stderr yourself ( ProcessStartInfo is your friend).

+3
source share

Check out FireDaemon. There is a free version (for example, FireDaemon lite) that only allows you to install one service at a time, but it is a very useful tool for configuring services. It also correctly wraps batch files, if necessary.

+1
source share

I am the second version of firedaemon. You can also set a parameter that allows the service to interact with the desktop so that it can display the cli output window. They no longer offer a free version, but if you search the Internet for firedaemon lite, you can find an older free version of Lite or maybe go on a toll road.

0
source share

All Articles