If you need to run a process as a daemon in Ubuntu, this guide will walk you through the steps to set it up properly using systemd
.
Setting Up the Daemon #
Let’s assume your executable is named arduino-data-points
, and you want to configure it as a daemon under the same name.
I assume the executable is stored here:
/root/arduino-data/arduino-data-points
1. Define the Service #
First, create a service file for systemd
.
sudo nano /etc/systemd/system/arduino-data.service
Modify the following parameters according to your setup:
- Description: A brief explanation of the service
- ExecStart: The full path to your executable
- WorkingDirectory: The directory where the service runs
- User: The user under which the service runs (e.g., root or another user)
- Group: The group under which the service runs (e.g., root or another group)
[Unit]
Description=Arduino Data Service
After=network.target
[Service]
ExecStart=/root/arduino-data/arduino-data-points
Restart=always
User=root
Group=root
WorkingDirectory=/root/arduino-data
[Install]
WantedBy=multi-user.target
2. Reload the Systemd Daemon #
After creating the service file, reload systemd
to apply the changes:
sudo systemctl daemon-reload
3. Start the Service #
Now, start the service:
sudo systemctl start arduino-data
4. Verify the Service Status #
To check if the service is running properly, use:
sudo systemctl status arduino-data
If successful, you should see an output similar to this:
● arduino-data.service - Arduino Data Service
Loaded: loaded (/etc/systemd/system/arduino-data.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2025-01-06 02:11:02 UTC; 1 month 3 days ago
Main PID: 13346 (arduino-data-po)
Tasks: 5 (limit: 1101)
Memory: 11.4M
CPU: 3min 33.196s
CGroup: /system.slice/arduino-data.service
└─13346 /root/arduino-data/arduino-data-points
5. Enable the Service to Start on Boot #
To ensure the service starts automatically on reboot, enable it with:
sudo systemctl enable arduino-data
You should see an output similar to:
Created symlink /etc/systemd/system/multi-user.target.wants/arduino-data.service → /etc/systemd/system/arduino-data.service.
Viewing Service Logs #
To check the service logs, use:
sudo journalctl -u arduino-data
Example output:
Jan 05 04:20:36 test-server systemd[1]: Started Arduino Data Service.
Jan 05 04:20:36 test-server arduino-data-points[3231]: 2025/01/05 04:20:36 Server started at :8081
Conclusion #
With these steps, your process should be successfully running as a daemon on Ubuntu, automatically restarting if needed and persisting through reboots.