Home Wiki Programming & Logic Services and systemd: Running Your Programs Automatically at Boot
Programming & Logic

Services and systemd: Running Your Programs Automatically at Boot

What Is a Service and Why You Need One

A service (daemon) is a program that runs continuously in the background. On a factory server, services power the Modbus gateway, web dashboard, and sensor collectors running 24/7.

systemd is the standard service manager on modern Linux, handling startup, crash recovery, logging, and resource limits automatically.

Managing Services: systemctl start, stop, and enable

sudo systemctl start nginx           # Start now
sudo systemctl stop nginx            # Stop now
sudo systemctl restart nginx         # Stop then start
sudo systemctl reload nginx          # Reload config without stopping
sudo systemctl enable --now nginx    # Enable at boot AND start now
sudo systemctl disable nginx         # Do not start at boot
systemctl status nginx               # Detailed status with recent logs
systemctl is-active nginx            # "active" or "inactive"
systemctl is-enabled nginx           # "enabled" or "disabled"
systemctl list-units --type=service --state=running

The status command shows whether the service is running, its PID, memory usage, and recent log lines.

Reading Logs: journalctl

journalctl -u nginx                              # All logs for a service
journalctl -u nginx --since "1 hour ago"         # Recent logs
journalctl -u nginx -n 50                        # Last 50 lines
journalctl -u nginx -f                           # Follow in real time
journalctl -u nginx -p err                       # Only errors

Writing a Custom .service File

Create /etc/systemd/system/sensor-collector.service:

[Unit]
Description=Industrial Sensor Data Collector
After=network.target

[Service]
Type=simple
User=scada
ExecStart=/opt/scada/collector/bin/sensor_collector
Restart=on-failure
RestartSec=5
MemoryMax=512M

[Install]
WantedBy=multi-user.target

Key directives: After sets startup order, Restart=on-failure auto-restarts on crashes, MemoryMax prevents runaway memory.

sudo systemctl daemon-reload         # Always run after modifying service files
sudo systemctl enable --now sensor-collector
journalctl -u sensor-collector -f

Timers: systemd Timers as a Modern cron Alternative

Create the service (/etc/systemd/system/backup-sensors.service):

[Unit]
Description=Backup sensor data

[Service]
Type=oneshot
User=scada
ExecStart=/opt/scada/scripts/backup_sensors.sh

Create the timer (/etc/systemd/system/backup-sensors.timer):

[Unit]
Description=Run sensor backup daily at 2 AM

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target
Schedule Meaning
*-*-* 02:00:00 Daily at 2 AM
Mon *-*-* 09:00:00 Mondays at 9 AM
*-*-* *:00/15:00 Every 15 minutes
sudo systemctl daemon-reload
sudo systemctl enable --now backup-sensors.timer
systemctl list-timers

Persistent=true runs missed jobs when the server starts after being off at scheduled time.

Practical Example: Creating a Service for a 24/7 Rust Monitoring App

sudo useradd -r -s /usr/sbin/nologin factory-monitor
sudo mkdir -p /opt/factory-monitor/bin
sudo cp target/release/factory-monitor /opt/factory-monitor/bin/
sudo chown -R factory-monitor:factory-monitor /opt/factory-monitor/

Create /etc/systemd/system/factory-monitor.service:

[Unit]
Description=Dr.Machine Factory Monitoring Application
After=network.target surrealdb.service

[Service]
Type=simple
User=factory-monitor
WorkingDirectory=/opt/factory-monitor
ExecStart=/opt/factory-monitor/bin/factory-monitor
Environment=RUST_LOG=info
Environment=DATABASE_URL=ws://localhost:8000
Restart=always
RestartSec=3
WatchdogSec=60
MemoryMax=1G
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now factory-monitor
systemctl status factory-monitor

WatchdogSec=60 expects a heartbeat every 60 seconds; if the app hangs, systemd restarts it.

Summary

In this lesson you learned services and timers on Linux:

  • Services run in the background with automatic startup and crash recovery.
  • systemctl starts, stops, enables, and monitors services.
  • journalctl queries logs by time, severity, and text patterns.
  • Custom .service files define resource limits, security, and restart behavior.
  • Systemd timers replace cron with better logging and persistence.
  • Industrial Rust apps should run as services with auto-restart and watchdog.

In the final lesson, you will build a complete industrial monitoring server from scratch.

systemd systemctl journalctl service daemon boot الخدمات التشغيل التلقائي الإقلاع السجلات إعادة التشغيل الخلفية