Basics of Service Management: systemctl
commands (starting, stopping, restarting services)
1. What are Services in Linux?
Services are the backbone of any modern Linux distro. Wanna run your web server on Nginx? Start and set up the corresponding service. Wanna get that MySQL database running? Yep, that's also a service. Even your fave cron
, which will execute those praise-worthy bash scripts, is a service too. Managing services is a skill you'll use not only at work but also for your personal projects. Oh, and by the way, during interviews, the question "So how would you stop a service?" pops up more often than you'd think.
Definition and Role
A service in Linux is basically a program that runs in the background (a.k.a. a daemon, derived from the English word "daemon"). These programs can auto-start when the system boots up or be started manually when needed. They handle specific tasks such as:
- Handling web requests (e.g.,
nginx
/apache
). - Supporting remote access (e.g.,
openssh
). - Running scheduled tasks (e.g.,
cron
).
Basically, services are the "workhorses" that handle tasks invisible to users but are crucial for the system's functionality.
2. Basics of Service Management Using systemctl
What is systemctl
?
Let's start with the fact that systemctl
is the main command for managing services in systems using systemd. If your system runs on a modern Linux distribution (like Ubuntu, Fedora, or CentOS), chances are it uses systemd
.
Main functions
- Starting a service: start a service if it's stopped.
- Stopping a service: stop a running service.
- Restarting a service: stop the service first, then restart it.
- Checking the status: find out the current status of a service.
Here's the basic command structure:
sudo systemctl [action] [service_name]
Starting and Stopping a Service
Starting:
Imagine you installed the nginx
web server and want to start it. This is where the start
command comes in handy:
sudo systemctl start nginx
Stopping:
But what if you want to temporarily disable the web server? The stop
command will save the day:
sudo systemctl stop nginx
Restarting a Service
Restarting is useful for applying configuration changes or just giving the service a "refresh" (basically, a cup of coffee to remind it it's time to work). Use the restart
command:
sudo systemctl restart nginx
Checking Service Status
Not sure if nginx
is running? The status
command gives you detailed information:
sudo systemctl status nginx
On output, you'll see:
- Process ID (PID).
- Start time.
- Error logs, if something went wrong.
Example output:
nginx.service - A high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-11-01 09:00:00 UTC; 10min ago
...
3. Service Auto-Start
Enable Auto-Start
To make a service start automatically every time the system boots up, use:
sudo systemctl enable nginx
Disable Auto-Start
If you want the service to stop starting automatically when the system boots:
sudo systemctl disable nginx
Here's a simple way to check if auto-start is enabled:
systemctl is-enabled nginx
Service Management Practice
Let's do a comprehensive exercise using the cron
service as an example.
Start the
cron
service:sudo systemctl start cron
Check if it's running:
sudo systemctl status cron
Make sure the status says "active (running)".
Stop the
cron
service:sudo systemctl stop cron
Check that the status has changed to "inactive (dead)".
Restart the
cron
service:sudo systemctl restart cron
4. The Difference Between Active, Disabled, and Failed Services
Services can have different statuses. Here are the main types:
- Active (running) — the service is actively running; everything’s good.
- Inactive (dead) — the service is stopped.
- Failed — there was an error during the start, stop, or execution of the service.
To quickly find all services that are in a failed state, use:
systemctl --failed
5. Common Errors and Pitfalls
Error: Unit not found
If you got a message like this:
Failed to start nginx.service: Unit nginx.service not found.
This might mean that the service you need is either not installed or its name is incorrect. Check the correct name:
systemctl list-unit-files | grep nginx
Permission Error
If you forgot to run the command with sudo
privileges, you'll most likely get an access denied error:
Permission denied
Don't forget to add sudo
before systemctl
commands.
6. Real-World Use Cases
Managing services is something you'll use in any Linux-related role:
- DevOps: automating and managing all system services.
- Server Administration: monitoring the status of web servers, databases, and other services.
- Linux Development: you need to understand how to manage services tied to your project (like starting development servers).
So, knowing the systemctl
command is not just an abstract skill but a real working tool. Example: need to restart a web server after a site update? One call to sudo systemctl restart nginx
— and problem solved!
GO TO FULL VERSION