Join Scott Simpson for an in-depth discussion in this video System basics: Service management, part of Linux Tips Weekly.
- [Instructor] On a Linux system, many of the useful features or programs that a system runs are called services. These services are programs like any that you can run from the command line. But they run in the background waiting for input, rather than being invoked right when they're needed and stopped when they're done. A web server, a file server, the system that keeps track of logs, the firewall, and many more useful functions are run as services. On most modern Linux systems, services are managed by systemd and we can interact with them using the systemctl command.
Systemctl shows us the status of services it knows about, and then gives us the opportunity to start and stop services and to enable and disable them. Or to tell the system whether or not to start a given service when the system boots up. The systemctl command by itself, gives us a big list of what's currently loaded. We can see the name, the status, whether each item is active, and a descriptive name that tells us what the item is. Each of these items is called a unit. And units are represented by files in the lib systemd system folder.
Let's take a look at one of them. Alright cat /lib/systemd/system, and let's take a look at rsyslog.service. That's the unit that manages the system log service. This service file describes the name and requirements and gives some documentation resources. And we can see what type the service is, what it runs when it's started. In this case it's actually starting the rsyslog daemon. And we see that it requests to be restarted whenever systemd detects that it's failed for some reason. Let's take a look at another one.
The ufw service for the firewall. This one is different. It's a one shot service. And it has instructions for both starting and stopping the service. We're not going to get into the details of writing a unit file here but I encourage you to explore the topic further if you're interested. You can add your own programs and services here to be managed through systemd. As I mentioned earlier, we can control the status of a service from the command line. To stop a service that's running I type, systemctl stop and the service name. Start brings the service up if it's not running. And restart stops the service and then starts it up again, saving a step of stopping and starting a service individually.
Enable marks the service to be started up when the system starts. And disable tells the system not to start a particular service when the machine comes up. Another useful command is status which shows a more detailed listing of the service's information. Let's use a systemctl status rsyslog to take a look at the system logger service. There's a little indicator here showing me the status. It's green which means the unit is active. This shows me the unit name and the description of what it is. And on the next line, I can see that the service is loaded.
I have the full path to the unit file and an indication that the service is enabled. Vendor preset shows what the supplier of the package has set as the default state. The active line shows me whether the unit is active or not, and whether it's running or not. For one shot services, or things that systemctl starts but isn't notified of later status for, you'll see active (exited) here instead of active (running). We see how long the unit has been running for as well. Then there's documentation information if it's provided in the unit file. And a display of the process ID, or PID for the unit.
You may see a number of tasks running under this process. And see how many megabytes of memory it's consuming and how many seconds of processor time it's used. And at the end of the top section, we see the process tree for the c group that the unit is running in. At the bottom of the section, we see recent log entries for the unit which can be really helpful for troubleshooting if something goes wrong. The systemctl utility makes managing services easy and consistent. Whether you're a system administrator, an enthusiast, or you just use Linux sometimes, it's a good thing to know about.
Note: Because this is an ongoing series, there is no certificate of completion available for this course.