Day 14 : 21 June 2022 : Running Ava as a Service on Linux

 My 100 Daze of Code

https://github.com/davidjwalling/100-days-of-code

#14 : Running Ava as a Service on Linux

Now that we have Ava running as a service on Windows, we can set up Ava to run as a background task, or "daemon", on Linux. We'll add code in Driver to handle the "service" program argument just as we did on Windows. Only for Linux, we'll fork the process, set up handlers to termination signals and close standard streams.

Once this code is tested, we can setup Ava to be started automatically by systemd by defining a service configuration file (ava.conf).

Installing Ava as a Daemon on Linux

The ava.service file is new and defines characteristics of the ava service.  This file is copied into the /lib/systemd/system/ folder. The executable will fork its process on startup. The initial program arguments passed include the "service" parameter. When systemd stops the service, it will issue a simple "kill" command on the main process ID. This corresponds to the SIGTERM signal, which the Ava library will now capture and exit Mainline. The /var/opt/ava folder is defined to be the current working directory (cwd) for the service. Log file output will be stored there.

The ava.conf file is stored in the /etc/modules-load.d/ folder. This file simply contains a list of services to start at startup.


The installation commands for these files and the program artifacts are shown here. Note that we install Ava and its library in different folders than our cmake installer does. This is so that we can stop a "production" version of the service running in /usr/bin, compile and install a test version in /usr/local/bin, and then promote the tested code manually. In this way, "sudo make install" will never overwrite a working production service copy.

Using systemctl to Start and Stop the Service

Here we start the ava service, stop the ava service, check the service status and then inspect the log file written to /var/opt/ava.


The logging output in /var/opt/ava indicates that the Termination signal (SIGTERM) was observed by the program when systemctl stopped the service. We see in the systemctl status output above that the exit code is "0/SUCCESS" which is expected. 


Finally, we'll run Ava as a service on Linux, but started from the command line. This is as simple as entering "ava service". The process will run in the background. We can confirm by checking our TCP listener. Then "kill" the service to send it a SIGTERM signal that will set _stop to true and exit Mainline to shut down.


Today's version of Ava will also run as a daemon on macOS if started from the command line. However, macOS uses launchd instead of systemd. So we'll look at installing Ava as a daemon on macOS a little later on.









Comments

Popular posts from this blog

Day 12 : 19 June 2022 : Adding Windows Service Logic

Day 5 : 12 June 2022 : CMake on Windows

Day 11: 18 June 2022 : Handling Program Arguments