Run web apps in the background

Windows service

To run an app in the background on Windows, you use a Service. Normally only console apps can be used as Service apps, but since web apps are actually subclassed from ConsoleApplication (technically they are subclassed from ServiceApplication, but that is a subclass of ConsoleApplication) they can easily be made to run as a Windows Service. The only tricky part is actually installing it as a Windows Service.

Windows has a built-in command for this, sc which standard for "service control".

Here are the steps to install your standalone web app as a Windows Service: 1. Build your app as a Standalone Web App and place the app in an accessible location. Be sure to note the port number that you used when building the app. You'll want to use a port number that is not already in use on the system. An example might be 8104.

  1. Start the Windows Command line app with administrator privileges

  2. Use the sc command to install the service as follows:

sc create XojoWebSvc type= own start= auto binpath= c:\\Path\\To\\Exe\\WebApp.exe
  1. After you press Return you should see: [SC] CreateService SUCCESS

  2. Now open up Control Panel and go to the Services Manager. It is located in the Administrative Tools section and is called Services.

  3. Find the service you just created. It will be listed by its app name. Click on it and select Start.

That's it. Your web application is now running as a service. To test it, navigate to the URL in your browser using the port you specified for the build:

http://localhost:8104

To stop the service, click on it in Service Manager and click Stop.

Linux daemon

A daemon is essentially a background process. You can use a daemon easily to deploy your Standalone web applications to remote web servers.

Here are steps to create a Standalone web app that can run as a daemon: 1. Add a single line of code to the App.Opening event handler (or App.Run for a Console app):

Call Daemonize
  1. Now build the app as Standalone and select a port number that is not in use on the machine. An example might be 8104. Copy the app to an accessible location.

  2. Open the Terminal and navigate to the location of your app.

  3. Start the app by typing its name in the Terminal:

./MyWebApp
  1. You will immediately return to the command line because the app is running as a daemon.

  2. Verify the daemon is running using the ps command:

ps -C MyWebApp

That's it. Your application is now running as a daemon. To test it, navigate to the URL in your browser:

http://localhost:8104

If you are remotely connected to a Linux server (using SSH), then disconnecting will also quit an app started in this manner. Instead you'll want to either start he app using the "&" suffix or using the nohup command.