0
0
Linux CLIscripting~5 mins

systemctl for service management in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing services on a Linux system can be confusing without the right tool. systemctl helps you start, stop, and check the status of services easily, so your programs run when you want them to.
When you want to start a web server so your website is available.
When you need to stop a service that is causing problems.
When you want to check if a service like SSH is running before connecting remotely.
When you want a service to start automatically when the computer boots.
When you want to restart a service after changing its configuration.
Commands
This command starts the nginx web server service immediately so it begins running.
Terminal
sudo systemctl start nginx
Expected OutputExpected
No output (command runs silently)
This command shows the current status of the nginx service, including if it is running or stopped.
Terminal
sudo 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-07 10:00:00 UTC; 2min 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 sets nginx to start automatically every time the system boots up.
Terminal
sudo systemctl enable nginx
Expected OutputExpected
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
This command stops the nginx service immediately, so it no longer runs.
Terminal
sudo systemctl stop nginx
Expected OutputExpected
No output (command runs silently)
This command stops and then starts the nginx service again, useful after changing its settings.
Terminal
sudo systemctl restart nginx
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from systemctl, remember: use start, stop, status, enable, and restart to control services easily.

Common Mistakes
Running systemctl commands without sudo.
Most service management commands require administrative rights and will fail without sudo.
Always prefix systemctl commands with sudo unless you are logged in as root.
Trying to start a service that is not installed.
systemctl cannot start services that do not exist, resulting in an error.
Check if the service is installed or available before starting it.
Forgetting to enable a service if you want it to start on boot.
The service will run now but will not start automatically after reboot.
Use 'sudo systemctl enable servicename' to make it start on boot.
Summary
Use 'sudo systemctl start servicename' to start a service immediately.
Check service status with 'sudo systemctl status servicename'.
Enable a service to start on boot with 'sudo systemctl enable servicename'.
Stop and restart services with 'sudo systemctl stop servicename' and 'sudo systemctl restart servicename'.