Application Service Maker - Run any program as a service

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
UPDATE 2013/03/23:

Version 0.3 released.

I've added some features which I've needed since the initial release. Maybe they'll be useful to others as well.

- Duplication is now possible, i.e. you can run the same application more than once, like multiple "cmd" processes.
- You can send arguments to an application.
- You can set an application to close when you stop the service.
- You can set the logging level to minimal (only logs errors) and verbose (logs pretty much everything that happens, for troubleshooting).

All these features can be accessed by changing the config file "ApplicationService.exe.config" in "C:\Program Files\Blaq Entertainment\Application Service Maker" or wherever you elected to install to. If you don't need the new features then there's no need to alter your current settings as they're backward compatible.

PLEASE MAKE A BACKUP OF YOUR CURRENT CONFIG FILE BEFORE UNINSTALLING OR UPDATING!

The new format for defining applications is

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ApplicationService.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <ApplicationService.Settings>
            <setting name="TimerIntervalMinutes" serializeAs="String">
                <value>20</value>
            </setting>
            <setting name="ApplicationList" serializeAs="String">
                <value>{app;path;can_duplicate;args;close_on_exit}</value>
            </setting>
            <setting name="LoggingMode" serializeAs="String">
                <value>M</value>
            </setting>
        </ApplicationService.Settings>
    </userSettings>
</configuration>

where
app is the name of the application to run (e.g. python)
path is the path to the application (e.g. C:\Python27\python.exe)
can_duplicate is either "Y" or "N" to indicate if the service can run the application even if it was already started.
arguments is whatever arguments you want to send to the application (e.g. "C:\Program Files\SickBeard\SickBeard.py")
close_on_exit is either "Y" or "N" to indicate if the service should close the application when the service is stopped

You can also see the "LoggingMode" setting, this should be "M" for minimal and "V" for verbose.

A few notes on duplications:
- If duplicate is set to "Y", then the service will run the application only if the currently running application instance wasn't started by the service. So if "python.exe" is already running and you start the service, another "python.exe" will be started by the service, but when the service checks for running applications after the interval, it will not run "python.exe" again (unless it stopped for some reason). The service will know which python it started by using process IDs.

- Normally if you use duplication, you'll probably want to set "close_on_exit" to "Y" as well, because when you stop the service, the service "forgets" which applications it started to it will start the process again when you start the service again.

Anywho, get the new shiny version here:
Installer: https://www.box.com/s/qm8fhfygcjw4rs1zwpbf
Source: https://www.box.com/s/2a3cnb4datb2ey6878h8


UPDATE 2013/03/20:

Version 0.2 released.

Not really much in this release, just a fix for Calibre's calibre-server not playing nice with the service.
Install: https://www.box.com/s/bbbdsscjafajo9cuzdwi
Source: https://www.box.com/s/j5qtizv2aq2pjhx6grzx


[Original Post]

Hi all,

I've written a service (C# Windows service) that allows you to run any program like a service.

First, some background:
I have a few programs that I want to always be running (SickBeard, CouchPotato, No-IP DUC and some others), and due to power cuts/surges/dips, my computer restarts quite often. I set the computer up so that it comes on automatically after a power cut, but the other problem is that unless I log into my user account, the programs won't start because they're not services.

So I decided to just make my own service that will run the programs automatically. You install the service and define a list of programs that you want to always be running, and the service will check periodically (interval can also be defined) if the programs are running and run any programs that are in your list and are not currently running. If a problem occurs while trying to run a program, the service will not try to run that program again until the service is restarted. It logs all errors and some messages to the event log.

I've been running it for 2 days now and it's working nicely :). I figured maybe it will be of use to others as well, and so I decided to release a version 0.1 beta. I'm also releasing the source code for scrutiny.

Installation:
Download the installer linked to below and run it. Select a download location and install. During installation you will need to enter the credentials of a user that you want to run the programs as (probably your account), and can write to the event log (an admin account).

Setup:
After installation, go to your installation directory (default C:\Program Files\Blaq Entertainment\Application Service Maker) and open the file ApplicationService.exe.config with notepad or any other text editor. Find the following section:
Code:
<ApplicationService.Settings>
	<setting name="TimerIntervalMinutes" serializeAs="String">
		<value>20</value>
	</setting>
	<setting name="ApplicationList" serializeAs="String">
		<value>{app1;path}{app2;path}</value>
	</setting>
</ApplicationService.Settings>
TimerIntervalMinutes is the time in minutes that the service should wait before checking/running programs.

ApplicationList is the list of programs that you want the service to check and run. The format is "{app;path}{app;path}{appn;path}".
"app" is the exe's name e.g. SickBeard (you can find this by opening the Windows task manager and clicking the processes tab).
"path" is the path to the exe (in the task manager, right click the process and select "Open file location").

Here are my current settings to give you a real example:
Code:
<ApplicationService.Settings>
	<setting name="TimerIntervalMinutes" serializeAs="String">
		<value>20</value>
	</setting>
	<setting name="ApplicationList" serializeAs="String">
		<value>{SickBeard;C:\SickBeard\SickBeard.exe}{DUC30;C:\Program Files (x86)\No-IP\DUC30.exe}{LogMeIn;C:\Program Files (x86)\LogMeIn\x64\LogMeIn.exe}</value>
	</setting>
</ApplicationService.Settings>
After configuring the settings, save the file and close it.
Now go to services (start, type "services.msc" and enter), find the service named "Application Service Maker", right click it and select "Start". You should then receive a call from a strange fellow named Bob, claiming to be your uncle.

Now what?:
At this point it might seem like nothing spectacular happened, but behind the scenes there was a bit of action.

Go to the event viewer, expand Windows Logs and then Application on the left. The service will write messages to the event viewer under the source "ApplicationService".

When the service starts, it will write
Code:
Application service started.
TimerIntervalMinutes: 20
Applications to start: 
SickBeard (C:\SickBeard\SickBeard.exe)
DUC30 (C:\Program Files (x86)\No-IP\DUC30.exe)
LogMeIn (C:\Program Files (x86)\LogMeIn\x64\LogMeIn.exe)
When the service runs an application, it will write
Code:
Attempting to start application: SickBeard (C:\SickBeard\SickBeard.exe)
If something goes wrong while trying to run a program, it will write (this will change depending on the error)
Code:
An error has occured in the application ApplicationService.
Error Location: ServiceStartApplications.StartApplication.
System.Exception: An error occurred when starting an application.
ApplicationName: SickBeard
PathToProcess: C:\SickBeard\SickBeard.exe
 ---> System.ComponentModel.Win32Exception: The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at ApplicationService.ServiceStartApplications.StartApplications()
   --- End of inner exception stack trace ---
If the program is already running, it will write
Code:
Application LogMeIn (C:\Program Files (x86)\LogMeIn\x64\LogMeIn.exe) has started, no need to start it.
There's a few other messages, but I'm sure you get the picture, the messages are meant to give you a good enough idea of what's going on.

Now your computer can restart and the service will automatically launch your programs.

Downloads:
Phew, this is probably my longest post yet :). Without further ado, here's some links.

Installer: http://www.box.com/s/3imdtksvyz2k89f7r2la
Source code: http://www.box.com/s/5qujeg4n61hfitbts18k
The source code is in a 7zip archive: http://www.7-zip.org/download.html

If the concept interests you, please give it a bash and leave your feedback here. I'll welcome all comments with a :). Enjoy!
 
Last edited:

Totempole

Expert Member
Joined
Sep 21, 2011
Messages
4,618
This is an interesting concept, but I can't think of many practical uses for it.

Question:

Let's assume I have multiple users on one PC. (True)
I download torrents quite frequently. (True)
I experience frequent power cuts. (Not true as yet)

So I use this application to run Bittorrent as a service. I leave for work,
and while I'm gone there's a power cut at home for 10 minutes.

The power comes back on and my PC automatically boots up to the login
screen.

Will Bittorrent automatically launch as a service and start downloading again even
though I'm not actually logged in?

Or do I have absolutely no idea what I'm talking about and this application serves
an entirely different purpose?
 

Mike Hoxbig

Honorary Master
Joined
Apr 25, 2010
Messages
39,684
This is an interesting concept, but I can't think of many practical uses for it.

Question:

Let's assume I have multiple users on one PC. (True)
I download torrents quite frequently. (True)
I experience frequent power cuts. (Not true as yet)

So I use this application to run Bittorrent as a service. I leave for work,
and while I'm gone there's a power cut at home for 10 minutes.

The power comes back on and my PC automatically boots up to the login
screen.

Will Bittorrent automatically launch as a service and start downloading again even
though I'm not actually logged in?

Or do I have absolutely no idea what I'm talking about and this application serves
an entirely different purpose?

It has practical use in business, for example when you need to restrict user access. Some applications require administrative privileges, and you don't necessarily want to give end users this. Running an application as a service with admin credentials will allow you to work around this.

Also, if you're thinking more enterprise, you can introduce redundancy to some business critical applications via a cluster if you have the application running as a service. I suppose it would be possible anyway by adding a generic application resource, but having it run as a service is just so much better for a bunch of reasons.

Lots of other reasons why you would want to do this. Thanks for sharing efitol - if I may ask, what's the difference between this and srvany that MS added as part of the Windows Resource Kit? Sorry I haven't done this in a while, but is it related to WRK not being available for Windows 7/Server 2008?
 

krono9

Senior Member
Joined
Sep 5, 2009
Messages
909
Will this be able to run dropbox as a service? I have comodo(which runs in service mode already) that makes an automatic backup to a dropbox, but always have to logon before the dropbox will sync online....
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
This is an interesting concept, but I can't think of many practical uses for it.

Question:

Let's assume I have multiple users on one PC. (True)
I download torrents quite frequently. (True)
I experience frequent power cuts. (Not true as yet)

So I use this application to run Bittorrent as a service. I leave for work,
and while I'm gone there's a power cut at home for 10 minutes.

The power comes back on and my PC automatically boots up to the login
screen.

Will Bittorrent automatically launch as a service and start downloading again even
though I'm not actually logged in?

Or do I have absolutely no idea what I'm talking about and this application serves
an entirely different purpose?
Yes, in that situation the service will be able to start your bittorrent client (let's say utorrent) automatically from the login screen without you having to log in, and utorrent will behave just as it would if you had launched in, i.e. start downloading your torrents.

It has practical use in business, for example when you need to restrict user access. Some applications require administrative privileges, and you don't necessarily want to give end users this. Running an application as a service with admin credentials will allow you to work around this.

Also, if you're thinking more enterprise, you can introduce redundancy to some business critical applications via a cluster if you have the application running as a service. I suppose it would be possible anyway by adding a generic application resource, but having it run as a service is just so much better for a bunch of reasons.

Lots of other reasons why you would want to do this. Thanks for sharing efitol - if I may ask, what's the difference between this and srvany that MS added as part of the Windows Resource Kit? Sorry I haven't done this in a while, but is it related to WRK not being available for Windows 7/Server 2008?
Thanks for the input.

To be honest I had not heard of srvany until now, but I had a quick look and it does seem to do the same thing. I suppose this service would be easier to setup than srvany for beginners as they won't need to prod the registry.

Will this be able to run dropbox as a service? I have comodo(which runs in service mode already) that makes an automatic backup to a dropbox, but always have to logon before the dropbox will sync online....
It should, yes.


Sent from my Galaxy Note
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
Third day now, and many dips have occurred due to the weather (I assume) and my programs are all running even though I have yet to log in to my server :D.

Thanks, this should probe useful for my future NAS

Cool, let us know how it goes.
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
Quite a few downloads but no feedback, so I'll just apply the HelloPeter principle and assume that it's working fine then ;)
 

Pixies

Honorary Master
Joined
Jul 18, 2008
Messages
56,654
This is useful for my server - we have automated backups going to a dropbox folder. I want to make sure dropbox always runs and does it's thing, so I'm going to run it with this and let you know how it goes.

So juts to understand it properly - does it run the app as an alias? Dropbox for example works off a user account.

Ta!
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
Yay, users :D

This is useful for my server - we have automated backups going to a dropbox folder. I want to make sure dropbox always runs and does it's thing, so I'm going to run it with this and let you know how it goes.

So juts to understand it properly - does it run the app as an alias? Dropbox for example works off a user account.

Ta!
The apps should run as whatever user you configure to run the service.

Nice concept! Will give it a try and see how it goes.
Thx
Cool, let us know.
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
How does this compare to FireDaemon? Is it a free version? :D

I suppose it's like FireDaemon but free, yes :D

I just needed something quickly and I was in the mood to do it myself so I did.
 

diabolus

Executive Member
Joined
Feb 4, 2005
Messages
6,310
I'll be having a look at this, i have alot of little things that need to be automated on the server and while i -can- write services in c# it's still much easier to write normal windows apps (with an actual screen) and have that run as services with pre-set parameters. Simple example is an app that needs to monitor a folder and pick up excel/csv files and move this into the database, quite often something fails and you need to be able to do this manually via the same app. I'm also not a big fan of having windows services handling alot of file related tasks , connecting to databases which often needs a user intervention , but it's still great if you got your app all working and can just load it into an app to "run as service" .

Currently i'm trialling AlwaysUp : http://www.coretechnologies.com/products/AlwaysUp/ , which costs money but works brilliantly. I suppose it's the same as FireDaemon.

So i will have a look at this too, since in the end this is something you'd use on multiple servers .
 

henro

New Member
Joined
Feb 26, 2013
Messages
2
Loooooooooove this thingie but.........................

Take a look at Belvedere. It works like a charm, especially in conjunction with this.


@efitol

I am using your app, it works like a charm, except for Calibre.....
When I run calibre-server from the command-line it works, running that same commandline through your service does not start the server.

I made sure that in both situations it was run under the same account (administrator in Windows 8)

I do not get any error-logging, just an information in application log saying it is trying to start calibre-server.
Any suggestions?

<value>{calibre-server;C:\Program Files (x86)\Calibre2\calibre-server.exe}{Belvedere;C:\Program Files (x86)\Belvedere\Belvedere.exe}{Dropbox;C:\Users\Henro\AppData\Roaming\Dropbox\bin\Dropbox.exe}</value>

C:\Program Files (x86)\Calibre2\calibre-server.exe runs great when started from commandline (as administrator and without elevated rights) and from WIN+R, again with and without administrative rights. It runs great when called from a batch file upon login. But not with your service...... Suggestions?:crying:

For now I am starting it like this:

http://www.mobileread.com/forums/showthread.php?t=74683
 
Last edited:

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
Thanks for the feedback henro, I'll have a look at Calibre and try to figure out why it isn't playing nice with the application.
 

henro

New Member
Joined
Feb 26, 2013
Messages
2
I'll have a look at Calibre and try to figure out why it isn't playing nice with the application.



Efitol,

the workaround I mentioned before doesn't work either so it may be that Calibre cannot run without user-interface? (It might be that I am talking gibberish, I ain't no developer :eek:)
 

Pixies

Honorary Master
Joined
Jul 18, 2008
Messages
56,654
Sounds about right henro. It likely needs a loaded user profile and registry hive to operate.
 

Bona Botse

A little insight goes a long way
Joined
Oct 10, 2005
Messages
5,227
Take a look at Belvedere. It works like a charm, especially in conjunction with this.


@efitol

I am using your app, it works like a charm, except for Calibre.....
When I run calibre-server from the command-line it works, running that same commandline through your service does not start the server.

I made sure that in both situations it was run under the same account (administrator in Windows 8)

I do not get any error-logging, just an information in application log saying it is trying to start calibre-server.
Any suggestions?

<value>{calibre-server;C:\Program Files (x86)\Calibre2\calibre-server.exe}{Belvedere;C:\Program Files (x86)\Belvedere\Belvedere.exe}{Dropbox;C:\Users\Henro\AppData\Roaming\Dropbox\bin\Dropbox.exe}</value>

C:\Program Files (x86)\Calibre2\calibre-server.exe runs great when started from commandline (as administrator and without elevated rights) and from WIN+R, again with and without administrative rights. It runs great when called from a batch file upon login. But not with your service...... Suggestions?:crying:

For now I am starting it like this:

http://www.mobileread.com/forums/showthread.php?t=74683

Very weird bug, this was. And I didn't really do anything to fix it, I just (eventually) loaded up the project, replicated the bug, rebuilt the solution and it went away.

Anyway, decided to release the "fix" as v0.2. I've updated the OP with the links to the new version.

Or download them here-
Install: https://www.box.com/s/bbbdsscjafajo9cuzdwi
Source: https://www.box.com/s/j5qtizv2aq2pjhx6grzx

If you ever get the time please test and let me know if this fixes it for you.
 

rph72

Executive Member
Joined
Jun 3, 2005
Messages
6,683
I'm trying to install this on a Windows 7 64 bit machine. The program doesn't want to accept my login credentials - Error 1001. :(
 
Top