From 41fd71911e1f6209369d87523b993c5cdceaacf6 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Fri, 30 Jan 2026 02:14:30 +0100 Subject: [PATCH] Added Readme --- README.md | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- systemd/MyExampleDaemon.conf | 4 ++++ systemd/MyExampleDaemon.service | 15 +++++++++++++++ systemd/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 systemd/MyExampleDaemon.conf create mode 100644 systemd/MyExampleDaemon.service create mode 100644 systemd/README.md diff --git a/README.md b/README.md index 4d83a29..f787a36 100644 --- a/README.md +++ b/README.md @@ -1 +1,92 @@ -# DaemonBase Library \ No newline at end of file +# DaemonBase Library + +Simple C++ header only template for creating Linux daemons + +### What are daemon services/processes ? +A dæmon is a program that runs silently in the background.
+Commonly, dæmon processes are created to offer a specific service. + +**Dæmon** processes usually: +- Live for a long time; +- Started at boot time; +- Terminate only during shutdown; +- Have no controlling terminal. + +[//]: # (### Dæmon Characteristics) + +[//]: # (The previously listed characteristics have certain implications:) + +[//]: # () +[//]: # (- do one thing, and one thing only) + +[//]: # (- resource leaks eventually surface) + +[//]: # (- consider current working directory) + +[//]: # (- no (or only limited) user-interaction possible) + +[//]: # (- how to create (debugging) output) + +### Usage +DaemonBase has a simple, straightforward api with callbacks to handle your daemon events. +```cpp +#include "daemonbase.h" +#include + +using namespace osdev::components::daemon; +using namespace std::chrono_literals; + +class MyExampleDaemon : public DaemonBase +{ +public: + void on_start(const daemonconfig &config) override + { + /// Called once after daemon starts automatically with system startup + /// or when you manually call `$ system,ctl start MyExampleDaemon` + + /// Initialize your code here... + + + + daemonlog::info("MyExampleDaemon::on_start(): MyExampleDaemon version: " + config.get("version") + " started successfully!"); + } + + void on_update() override + { + /// Called every DURATION set in set_update_duration()... + + /// Update your code here + + daemonlog::info("MyExampleDaemon::on_update()"); + } + + void on_stop() override + { + /// Called once before daemon is about to exit with system shutdown or when you manually call `$ systemctl stop MyExampleDaemon` + /// Cleanup your code here... + + daemonlog::info("MyExampleDaemon::on_stop()"); + } + + void on_reload(const daemonconfig &cfg) override + { + /// Called once after your daemon's config fil is updated then reloaded with `$ systemctl reload MyExampleDaemon` + /// Handle your config updates here... + + daemonlog::info("MyExampleDaemon::on_reload(): new daemon version from updated config: " + cfg.get("version")); + } +}; + + +int main(int argc, char *argv[]) +{ + MyExampleDaemon oDaemon; // Create the daemon instance. + + oDaemon.set_name("MyAweSomeExampleDaemon"); // Set daemon name to identify logs in syslog + oDaemon.set_update_duration(3s); // Set duration to sleep before triggering the on_update callback 3 seconds. + oDaemon.set_cwd("/root"); // set daemon's current working directory to roots home-folder + oDaemon.run(argc, argv); // run the daemon + + return(EXIT_SUCCESS); // Close the main process now the child process is running. +} +``` \ No newline at end of file diff --git a/systemd/MyExampleDaemon.conf b/systemd/MyExampleDaemon.conf new file mode 100644 index 0000000..3599052 --- /dev/null +++ b/systemd/MyExampleDaemon.conf @@ -0,0 +1,4 @@ +# here you can have your daemon configuration +name=@PROJECT_NAME@ +version=@PROJECT_VERSION@ +description=@PROJECT_DESCRIPTION@ \ No newline at end of file diff --git a/systemd/MyExampleDaemon.service b/systemd/MyExampleDaemon.service new file mode 100644 index 0000000..b82dc87 --- /dev/null +++ b/systemd/MyExampleDaemon.service @@ -0,0 +1,15 @@ +# Properties docs: https://www.freedesktop.org/software/systemd/man/systemd.service.html +[Unit] +Description=@PROJECT_DESCRIPTION@ +After=network.target + +[Service] +Type=forking +ExecStart=/usr/bin/@PROJECT_NAME@ --config /etc/@PROJECT_NAME@/@PROJECT_NAME@.conf +ExecReload=/bin/kill -s SIGHUP $MAINPID +ExecStop=/bin/kill -s SIGTERM $MAINPID +User=root +SyslogIdentifier=@PROJECT_NAME@ + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/systemd/README.md b/systemd/README.md new file mode 100644 index 0000000..f5ce1b4 --- /dev/null +++ b/systemd/README.md @@ -0,0 +1,38 @@ +Linux systems that use systemd for managing services, .service files are typically placed in the /lib/systemd/system/ directory or the /etc/systemd/system/ directory. The .conf files may be placed in a similar directory, such as /etc/MyExampleDaemon/. + +### .service +```ini +# Properties docs: https://www.freedesktop.org/software/systemd/man/systemd.service.html +[Unit] +Description=Simple C++ template example for creating Linux daemons +After=network.target + +[Service] +# Configures the process start-up type for this service unit. One of simple, exec, forking, oneshot, dbus, notify or idle. +# https://unix.stackexchange.com/questions/733890/systemd-service-unit-restart-on-failure-doesnt-restart-daemon +Type=forking +# when systemctl start is called +ExecStart=/usr/bin/MyExampleDaemon --config /etc/MyExampleDaemon/MyExampleDaemon.conf +# when systemctl reload MyExampleDaemon (for reloading of the service's configuration) it will trigger SIGHUP +# which will be caught by signal_handler and trigger the on_reload callback. +ExecReload=/bin/kill -s SIGHUP $MAINPID +# when systemctl stop MyExampleDaemon called: Will trigger SIGTERM which will be caught by signal_handler +# and trigger the on_stop callback. +ExecStop=/bin/kill -s SIGTERM $MAINPID +User=root +StandardError=syslog +SyslogIdentifier=MyExampleDaemon + +[Install] +# Start after boot +WantedBy=multi-user.target +``` + + +### .conf +```ini +# here you can have your daemon configuration +name=MyExampleDaemon +version=0.0.1 +description=Simple C++ template example for creating Linux daemons +``` -- libgit2 0.21.4