0
0
Linux CLIscripting~5 mins

Startup and init systems (systemd) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you turn on a Linux computer, many programs need to start automatically. Systemd is the tool that manages these programs, making sure they start in the right order and keep running. It helps your system boot up smoothly and keeps services running.
When you want a web server to start automatically after your Linux machine boots.
When you need to check if a background service like a database is running.
When you want to stop or restart a service without rebooting the whole system.
When you want to enable a custom script to run at startup.
When you want to see the status or logs of a service to troubleshoot problems.
Commands
This command starts the nginx web server immediately. Use it when you want to run the service now without rebooting.
Terminal
systemctl start nginx
Expected OutputExpected
No output (command runs silently)
This shows if the nginx service is running, stopped, or has errors. It helps you check the current state of the service.
Terminal
systemctl status nginx
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-14 10:00:00 UTC; 5min ago Main PID: 1234 (nginx) Tasks: 3 (limit: 4915) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─1235 nginx: worker process └─1236 nginx: worker process
This command makes sure nginx starts automatically every time the system boots. Use it to keep the service running after reboots.
Terminal
systemctl enable nginx
Expected OutputExpected
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
This stops nginx from starting automatically on boot. Use it if you want to prevent the service from running after reboot.
Terminal
systemctl disable nginx
Expected OutputExpected
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.
This shows the logs for the nginx service from the last 5 minutes. Logs help you understand what the service did or if it had any errors.
Terminal
journalctl -u nginx --since "5 minutes ago"
Expected OutputExpected
Jun 14 10:00:00 my-server systemd[1]: Started A high performance web server and a reverse proxy server. Jun 14 10:00:01 my-server nginx[1234]: nginx started successfully.
-u - Specifies the service unit to show logs for
--since - Shows logs starting from a specific time
Key Concept

If you remember nothing else from this pattern, remember: systemd controls starting, stopping, enabling, and checking services on Linux.

Common Mistakes
Trying to start a service without root or sudo privileges
Starting or managing services usually requires administrator rights, so the command will fail with permission denied.
Use sudo before the systemctl command, for example: sudo systemctl start nginx
Forgetting to enable a service after starting it
The service will run now but will not start automatically after reboot, causing downtime after restarts.
Run systemctl enable service_name to make it start on boot.
Using systemctl status without checking logs when a service fails
Status shows if the service is running but not detailed error messages, so troubleshooting is incomplete.
Use journalctl -u service_name to see detailed logs and find the cause of failure.
Summary
Use systemctl start to run a service immediately.
Use systemctl enable to make a service start automatically on boot.
Use systemctl status and journalctl to check service health and logs.