To manage Linux services effectively, use systemctl with systemd. 1. Start a service immediately with sudo systemctl start nginx.service. 2. Stop it with sudo systemctl stop nginx.service. 3. Restart using sudo systemctl restart nginx.service. 4. Reload configuration without restart via sudo systemctl reload nginx.service. 5. Enable at boot with sudo systemctl enable nginx.service or combine with start using enable --now. 6. Check status using systemctl status sshd.service. 7. List active services with systemctl list-units --type=service --state=active or all loaded services with --all. 8. View logs via sudo journalctl -u nginx.service, adding -f to follow in real time. For custom services, create a .service file in /etc/systemd/system/, then run sudo systemctl daemon-reload before enabling or starting. Troubleshoot failures by checking status and logs, ensuring correct file location and permissions, verifying executable paths, and adjusting service type for non-daemon scripts. Mastering these systemctl and journalctl commands provides a robust foundation for service management in production environments.
Managing Linux services effectively is a core skill for system administrators and developers working with modern Linux distributions. Most of today’s Linux systems use systemd as the init system — the first process that starts at boot and manages all other services. The primary tool for interacting with systemd is systemctl
. Here's how to use it to manage services efficiently.

Understanding systemd and systemctl
systemd is a system and service manager for Linux that provides a standardized way to manage daemons, boot processes, logging, and more. It replaces older init systems like SysVinit. The systemctl
command is the main interface for controlling systemd.
Services in systemd are defined by unit files (usually ending in .service
), typically located in /etc/systemd/system/
or /usr/lib/systemd/system/
. These files define how a service should start, stop, and behave.

Common systemctl Commands for Service Management
Here are the essential systemctl
commands you’ll use daily:
-
Start a service immediately
sudo systemctl start nginx.service
This runs the service now but doesn’t enable it to start at boot.
Stop a running service
sudo systemctl stop nginx.service
Restart a service
sudo systemctl restart nginx.service
Reload a service’s configuration (without full restart)
sudo systemctl reload nginx.service
Useful for daemons like web servers when you change config files.
Enable a service to start at boot
sudo systemctl enable nginx.service
Creates a symbolic link to the service unit in the appropriate boot target.
Disable a service from starting at boot
sudo systemctl disable nginx.service
Note: You can combine enable and start in one command:
sudo systemctl enable --now nginx.service
Checking Service Status and Logs
Knowing the current state of a service is crucial.
Check if a service is running
systemctl status sshd.service
This shows active/inactive status, process ID, recent logs, and whether it’s enabled.
List all active services
systemctl list-units --type=service --state=active
List all loaded services (including inactive ones)
systemctl list-units --type=service --all
View logs for a service using journalctl
sudo journalctl -u nginx.service
Add
-f
to follow logs in real time:sudo journalctl -u nginx.service -f
Working with Custom or Third-Party Services
If you're deploying your own application, you might need to create a custom service file.
Example: /etc/systemd/system/myapp.service
[Unit] Description=My Custom Application After=network.target [Service] Type=simple User=myuser ExecStart=/opt/myapp/start.sh Restart=on-failure [Install] WantedBy=multi-user.target
After creating the file:
sudo systemctl daemon-reload sudo systemctl enable myapp.service sudo systemctl start myapp.service
Always run
daemon-reload
after adding or modifying a service file so systemd reloads the configuration.
Troubleshooting Common Issues
Service fails to start?
Check the status and logs:systemctl status myapp.service journalctl -u myapp.service --since "10 minutes ago"
Service not found?
Make sure the.service
file is in the right directory and you’ve rundaemon-reload
.Permissions or path issues?
Ensure scripts or binaries specified inExecStart
are executable and paths are absolute.Service starts but dies immediately?
Consider usingType=oneshot
orRemainAfterExit=yes
for non-daemon scripts, or fix crashes in the application.
Managing services with systemctl
is straightforward once you know the key commands. Whether you're starting, stopping, enabling, or debugging services, systemd provides a consistent and powerful framework. Getting comfortable with systemctl
and journalctl
will save you time and reduce downtime in production environments.
Basically, just remember: start, stop, enable, status, and logs — that covers 90% of what you’ll need.
The above is the detailed content of Managing Linux Services with systemd and systemctl. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

LXD is described as the next-generation container and virtual machine manager that offers an immersive for Linux systems running inside containers or as virtual machines. It provides images for an inordinate number of Linux distributions with support

Clear Linux OS is the ideal operating system for people – ahem system admins – who want to have a minimal, secure, and reliable Linux distribution. It is optimized for the Intel architecture, which means that running Clear Linux OS on AMD sys

The key steps for creating a self-signed SSL certificate are as follows: 1. Generate the private key, use the command opensslgenrsa-outselfsigned.key2048 to generate a 2048-bit RSA private key file, optional parameter -aes256 to achieve password protection; 2. Create a certificate request (CSR), run opensslreq-new-keyselfsigned.key-outselfsigned.csr and fill in the relevant information, especially the "CommonName" field; 3. Generate the certificate by self-signed, and use opensslx509-req-days365-inselfsigned.csr-signk

Firefox browser is the default browser for most modern Linux distributions such as Ubuntu, Mint, and Fedora. Initially, its performance might be impressive, however, with the passage of time, you might notice that your browser is not as fast and resp

Decompress the .zip file on Windows, you can right-click to select "Extract All", while the .tar.gz file needs to use tools such as 7-Zip or WinRAR; on macOS and Linux, the .zip file can be double-clicked or unzip commanded, and the .tar.gz file can be decompressed by tar command or double-clicked directly. The specific steps are: 1. Windows processing.zip file: right-click → "Extract All"; 2. Windows processing.tar.gz file: Install third-party tools → right-click to decompress; 3. macOS/Linux processing.zip file: double-click or run unzipfilename.zip; 4. macOS/Linux processing.tar

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.
