Sistem Yönetimi
100%

Linux Systemd: Managing Services and Daemons

A comprehensive guide to understanding systemd, creating custom service files, and managing background processes in modern Linux distributions.

Overview

Systemd is an init system and system manager that has become the standard for modern Linux distributions (Ubuntu, CentOS, RHEL). It is responsible for initializing the system during boot and managing long-running background processes, known as daemons.

The Problem

When you deploy a custom web application or a backend script on a Linux server, you need it to run continuously in the background. If the server reboots or the application crashes due to an error, you don't want to log in via SSH and manually start the script again. The old SysVinit system used complex and hard-to-maintain bash scripts for this purpose.

Solution and Configuration

Systemd solves this through declarative configuration files called Unit files. You can create a systemd service file to tell the OS exactly how to start, stop, and restart your application.

Example /etc/systemd/system/myapp.service:

[Unit]
Description=My Node.js Application
After=network.target

[Service]
ExecStart=/usr/bin/node /opt/myapp/app.js
Restart=always
User=deployer
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Apply changes and enable it to start on boot: sudo systemctl enable --now myapp

Technical Details

Systemd heavily utilizes parallelization to speed up boot times. It relies on Unit files which can represent services (.service), sockets (.socket), timers (.timer, a modern alternative to cron), or mount points. The [Service] section defines the lifecycle. The directive Restart=always ensures that systemd monitors the PID (Process ID) and automatically revives it if it terminates unexpectedly. All logs generated by the standard output (stdout/stderr) of systemd services are centralized and easily queryable using the journalctl command.

Conclusion

Mastering systemctl and creating systemd unit files is a fundamental skill for any system administrator or DevOps engineer. It provides a robust, standardized way to ensure applications remain highly available and resilient on Linux environments.

Related Articles

View All