Run Web Apps in the Background
From Xojo Documentation
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:
- 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.
- Start the Windows Command line app with administrator privileges
- Use the sc command to install the service as follows:
sc create XojoWebSvc type= own start= auto binpath= c:\Path\To\Exe\WebApp.exe
- After you press Return you should see: [SC] CreateService SUCCESS
- Now open up Control Panel and go to the Services Manager. It is located in the Administrative Tools section and is called Services.
- 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:
To stop the service, click on it in Service Manager and click Stop.
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:
- Add a single line of code to the App.Open event handler (or App.Run for a Console app):Call Daemonize
- 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.
- Open the Terminal and navigate to the location of your app.
- Start the app by typing its name in the Terminal:
- You will immediately return to the command line because the app is running as a daemon.
- 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:
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.