Run any program as a Windows service in the background using WinSW
Sometimes it would be useful to run a Windows program in the background as a Windows service, although the program was developed to run on the desktop or command line. Although no GUI window can then be displayed, the application will run even if the user is logged off. Suitable applications must of course be able to work independently without user interaction, after the settings have been set accordingly beforehand.
We will demonstrate this here with the backup program Live File Backup, which was developed to backup modified work files continuously and versioned in the background between the full backups. Although it was designed as a desktop application, with this procedure it can run in the background even if the user has logged off – e.g. if files of other users or external disks are still to be backed up while logged off.
Live File Backup is normally controlled by an icon in the system tray of the taskbar. Since this icon, or a corresponding window, will not be present when running as a service, the following must be considered:
- For configuration changes the service must be stopped and uninstalled, then for changing the settings the program must be run as a desktop application and stopped again, then reinstalled and started as a service. As you will see, this is easy to do.
- The program function to start the application directly automatically with Windows cannot be used, because otherwise two instances of the program would run at the same time. But this is not a problem, because the application will be started with Windows as a service anyway.
Now we come to the application of the small but ingenious Windows tool that can run almost any desktop program as a service in the background: It’s called WinSW, Windows Service Wrapper, and it was developed as open source by Kohsuke Kawaguchi, Oleg Nenashev and many other contributors. All details can be found on GitHub at: https://github.com/winsw/winsw .
Step 1: Install and configure the affected desktop program
We download and run the installer for our sample application Live File Backup: https://www.livefilebackup.com
After that, the program is available as an icon in the notification area of the taskbar (SysTray). Afterwards we set all program settings so that it works as a normal desktop application according to our wishes. Instructions for Live File Backup as a PDF document: https://www.livefilebackup.com/public/LiveFileBackupEN.pdf
Of course, any other desktop or command line program that can do its work independently without user interaction can be used for this purpose.
Step 2: Install the tool WinSW
We download and install the WinSW tool, the latest version at the time of this blog is 2.11.0: https://github.com/winsw/winsw/releases/tag/v2.11.0
We need the executable program “WinSW-x64.exe” and the configuration file “sample-allOptions.xml”. We save both in a directory like “C:\WinSW”, for example. Then we give both files any meaningful but same name, e.g. “LFBservice.exe” and “LFBservice.xml”.
Step 3: Configure WinSW
WinSW is configured via the XML file; so we open “LFBservice.xml” in a text editor like Notepad. The full documentation can be found at: https://github.com/winsw/winsw/blob/v3/docs/xml-config-file.md
Most of the settings in this file are commented out by
<!-- ... --> because they are not needed. For the following relevant entries, we remove the commenting and/or adjust the settings as follows.
ID, name and description for the intended service:
<!-- ID of the service. It should be unique accross the Windows system-->
<!-- Display name of the service -->
<!-- Service description -->
<description>Live File Backup as a service</description>
Path to the application we want to install as a service:
<!-- Path to the executable, which should be started -->
<!-- <executable>%BASE%\myExecutable.exe</executable> -->
<executable>C:\Program Files (x86)\LiveFileBackup\LiveFileBackup.exe</executable>
Many desktop programs run only under the user account in which they were installed:
(Alternatives are LocalSystem, LocalService, NetworkService. See above mentioned documentation.)
Defines account, under which the service should run.
In addition to these settings, there are many other interesting options, but they are normally not necessary for the basic function.
Step 4: Install and start the service
After saving the settings in the XML file, we can now install and start the new service. We open a Command prompt or a PowerShell window, preferentially with administrator rights so that the UAC does not request admin rights at every step.
In the command prompt, we change to our appropriate directory, such as “C:\WinSW”.
With the following command we create the new service:
And then we start the new service:
From this point on, the new service, and thus our application, will be running whenever Windows is started.
If we open the Windows Services interface (click Start and type “Services”), we can also find and control the new service there.
The service can be stopped and uninstalled with the following commands:
As already mentioned, the service must first be stopped and uninstalled for changes to the application settings, and after the changes the application must be closed, the service reinstalled and started again. But this is easily and quickly done with the above commands.
With WinSW almost any program that works independently can be turned into a Windows background service so that it runs even when no user is logged in. For more details please be sure to look at the documentation mentioned above.