| slug |
title |
authors |
tags |
| Managing Systemd Services and Units with Systemctl A Guide |
Managing Systemd Services and Units with Systemctl A Guide |
|
|
import CodeBlock from '@site/src/components/CodeBloack';
Introduction
Systemd is an important part of many Linux computers. It acts like an organizer that helps your computer work smoothly. Understanding systemd might seem a bit challenging at first, but it's a powerful tool that simplifies managing your computer. Learning about systemd and its tools will give you more control and flexibility over your computer. It's like having a reliable assistant that makes your computer tasks easier. So, let's explore systemd together and see how it can become your computer's helpful companion!
In this guide, we'll explore a command called 'systemctl,' a powerful tool for managing how your computer starts and runs programs. We'll cover how to control programs, check their status, make changes and work with special files that control how everything functions.
Just so you know, not all computers use the same tools. If you come across a message like 'bash: systemctl is not installed,' it means your computer has a different setup. No worries, though – you can still learn a lot from this guide!
Managing Services with Systemctl
The init system is like the manager of your computer. Its main job is to get things ready after you turn on your computer, which we call 'userland' components. It also looks after special helpers called services and daemons while your computer is already up and running. Now, let's jump into some simple ways to take care of these services – it's like learning how to manage different tasks on your computer!
In systemd, everything we want to control is called a 'unit.' Units are like different parts of your computer that systemd knows how to handle. Each unit is put into a group based on what it does, and we describe them using files called unit files. You can tell what type of unit it is by looking at the end of its file name.
When you're managing services, you'll mainly deal with something called 'service units.' These units have files with names ending in .service. But here's something cool: most of the time, you can just leave off the .service part when you're giving commands to systemd. It's clever enough to figure out that you're probably talking about a service!
Getting Services Going and Turning Them Off
To get a systemd service up and running, you use the start command. If you're not the main user of the computer, you'll need to add sudo before the command, because starting a service can change how the computer works:
<CodeBlock code={sudo systemctl start application.service} />
As we mentioned earlier, when you're dealing with services in systemd, you usually don't need to worry about typing the ".service" part. So, you can just type the command like this:
<CodeBlock code={sudo systemctl start application} />
While you can use the simplified command we mentioned earlier for most things, for better clarity in our instructions, we'll include the ".service" part in the following commands.
To turn off a service that's currently running, use the stop command instead:
sudo systemctl stop application.service
Refreshing and Rebooting Services
To give a running service a fresh start, you can use the restart command:
<CodeBlock code={sudo systemctl restart application.service} />
If the program you're using can update its settings without needing to restart, you can use the reload command to make that happen:
<CodeBlock code={sudo systemctl reload application.service} />
If you're not sure whether the service can update its settings without restarting, you can use the reload-or-restart command. This smart command will try to update the settings in place if possible. If not, it will simply restart the service with the new settings.
<CodeBlock code={sudo systemctl reload-or-restart application.service} />
Turning Services On and Off
The commands we talked about earlier are handy for turning services on or off while you're using your computer. But if you want a service to start automatically every time you turn on your computer, you need to tell systemd to do that.
To make a service start automatically when you boot up your computer, use the enable command:
<CodeBlock code={sudo systemctl enable application.service} />
When you use the enable command, it basically creates a special link. This link points from where your computer usually keeps track of services (like in /lib/systemd/system or /etc/systemd/system) to the place where systemd checks for things to start automatically (usually /etc/systemd/system/some_target.target.wants).
Now, if you change your mind and don't want a service to start automatically anymore, you can type:
<CodeBlock code={sudo systemctl disable application.service} />
This will simply undo the special link that makes the service start automatically.
Remember, when you enable a service, it doesn't start running right away. If you want it to start now and also whenever you turn on your computer, you'll need to use both the start and enable commands.
Checking How Services Are Doing
To see how a service is doing on your computer, you can use the status command:
<CodeBlock code={systemctl status application.service} />
This will give you information about the service, like whether it's running or not, where it's organized in the system, and some early details from its logs.
For example, if you check the status of an Nginx server, you might see something like this:
<CodeBlock code={Output ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2015-01-27 19:41:23 EST; 22h ago Main PID: 495 (nginx) CGroup: /system.slice/nginx.service ├─495 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; └─496 nginx: worker process Jan 27 19:41:23 desktop systemd[1]: Starting A high performance web server and a reverse proxy server... Jan 27 19:41:23 desktop systemd[1]: Started A high performance web server and a reverse proxy server. } />
This gives you a helpful summary of how the application is doing, letting you know if there are any issues or if anything needs your attention.
If you want to check if a unit (like a service) is currently doing its job (running), you can use the is-active command: