Language: EN

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

How to Use and Create Daemon on Raspberry Pi

In Unix and Linux systems, a daemon is a process that runs in the background and is not associated with a direct user interface.

These processes are usually started during system boot and continue running until the system is shut 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 operate:

  1. Initialization: The daemon is started when the system boots or when explicitly requested. It can be initiated from a startup script, such as systemd, or through a configuration file in /etc/init.d/.

  2. Detachment from 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 to operate and perform 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 through system shutdown, a specific command, or when a critical error occurs.

How to Use Daemons on 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 you to start, stop, enable, and monitor system services.

You can use the following commands to control daemons:

Start a Daemon:

sudo systemctl start service_name

Stop a Daemon:

sudo systemctl stop service_name

Restart a Daemon:

sudo systemctl restart service_name

Enable a Daemon to start automatically at boot:

sudo systemctl enable service_name

Disable a Daemon from starting automatically:

sudo systemctl disable service_name

Check the status of a Daemon:

sudo systemctl status service_name

Troubleshooting

To diagnose issues with a daemon, check the logs:

journalctl -u service_name

Create 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/my_daemon.service

In this file, the behavior of the service is defined. Next, we add the necessary configuration. In the my_daemon.service file, add the following configuration:

[Unit]
Description=My Custom Daemon

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

[Install]
WantedBy=multi-user.target
  • [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 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, meaning a multi-user system without a GUI.

After creating or modifying a service file, you need to reload systemd to read the new configuration:

sudo systemctl daemon-reload

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

sudo systemctl start my_daemon
sudo systemctl enable my_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 my_daemon

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