como-usar-y-crear-daemon-en-raspberry-pi

How to Use and Create Daemon on Raspberry Pi

  • 4 min

In Unix and Linux systems, a daemon (or “demon” in Spanish) is a process that runs in the background and is not associated with a direct user interface.

These processes typically start during system boot and continue running until the system shuts down.

Daemons perform essential tasks, such as managing network services, handling print requests, or monitoring system resources.

How Daemons Work

A daemon generally follows these steps to function:

  1. Initialization: The daemon starts at system boot or when explicitly requested. It can be started from a startup script, like systemd, or through a configuration file in /etc/init.d/.

  2. Detachment from the Terminal: The daemon detaches from the terminal that started it. This means the process runs in the background and is not tied to a specific user session.

  3. Continuous Execution: Once running, the daemon continues functioning and performing its specific task. It does not require user intervention and can operate even if no users are logged in.

  4. Termination: Daemons can be terminated in various ways, either by system shutdown, via a specific command, or when a critical error is encountered.

How to Use Daemons in Raspberry Pi

In Raspberry Pi OS, which uses systemd to manage services. systemd is a service management system used in many modern Linux distributions. It allows starting, stopping, enabling, and monitoring system services.

You can use the following commands to control daemons:

Start a Daemon:

sudo systemctl start nombre_del_servicio

Stop a Daemon:

sudo systemctl stop nombre_del_servicio

Restart a Daemon:

sudo systemctl restart nombre_del_servicio

Enable a Daemon to start automatically at boot:

sudo systemctl enable nombre_del_servicio

Disable a Daemon from starting automatically:

sudo systemctl disable nombre_del_servicio

Check the status of a Daemon:

sudo systemctl status nombre_del_servicio

Troubleshooting

To diagnose problems with a daemon, check the logs:

journalctl -u nombre_del_servicio

Creating a Custom Daemon

If you want to create a custom Daemon, you need to define a service file for systemd. First, you must create a service file that will define its configuration.

To do this, open a text editor with superuser permissions:

sudo nano /etc/systemd/system/mi_daemon.service

In this file, the service’s behavior is defined. Next, we add the necessary configuration. In the mi_daemon.service file, add the following configuration:

[Unit]
Description=My Custom Daemon

[Service]
ExecStart=/path/to/executable
Restart=always

[Install]
WantedBy=multi-user.target
Copied!
  • [Unit]: Section that provides the service description.
    • Description: Brief description of the service.
  • [Service]: Section that defines how the service should behave.
    • ExecStart: Full path to the daemon’s executable.
    • Restart: Defines the service restart policy; always will restart the service if it stops unexpectedly.
  • [Install]: Section that specifies the conditions for enabling the service.
    • WantedBy: Defines the target to which this service belongs; multi-user.target is equivalent to runlevel 3, i.e., a multi-user system without a GUI.

After creating or modifying a service file, it is necessary to reload systemd so it reads the new configuration:

sudo systemctl daemon-reload

Now you can start the service and make it start automatically at system boot:

sudo systemctl start mi_daemon sudo systemctl enable mi_daemon

  • start: Starts the service immediately.
  • enable: Enables the service to start automatically at system boot.

To ensure the service is running correctly, you can check its status with the following command:

sudo systemctl status mi_daemon

This command will show the current status of the service, as well as any errors or log messages that may help you diagnose problems.