Complete Guide: Systemctl Commands and Service Management Tips

Article directory

  • Introduction
    • 1.1 What is the systemctl command?
    • 1.2 The role and use of systemctl
    • 1.3 The difference between systemctl and other similar commands
  • Basic usage
    • 2.1 Systemctl syntax and common options
    • 2.2 Starting and stopping services
    • 2.3 Restart and reload the service
    • 2.4 Check service status
    • 2.5 Set the default startup level
  • Service management
    • 3.1 List all enabled services
    • 3.2 Enabling and disabling services
    • 3.3 Adding and removing services
    • 3.4 Customized service configuration
  • Dependency management
    • 4.1 Dependencies between services
    • 4.2 Solving dependency issues
    • 4.3 Configure service startup sequence
  • Log management
    • 5.1 View service logs
    • 5.2 Set log level and storage location
    • 5.3 Log rotation and compression
  • Commonly used tips and examples
    • 6.1 Use wildcards to batch operate services
    • 6.2 Start and stop services regularly
    • 6.3 Customize services through systemd unit files
    • 6.4 Process Control and Resource Limitations
  • Troubleshooting and FAQ
    • 7.1 Find and solve the reasons for service startup failure
    • 7.2 Solve the problem that the service cannot be stopped or reloaded
    • 7.3 Handling log overflow and write errors
  • Summarize
  • Recommended Python boutique columns
    • Basic knowledge of python (0 basic introduction)
    • python crawler knowledge

Introduction

1.1 What is the systemctl command

systemctl is a command used to manage system services in Linux systems. It is part of the systemd initialization system and provides powerful management functions for system processes by controlling and monitoring system services and unit files.

1.2 The function and use of systemctl

systemctl can be used to start, stop, restart and reload services, view service status and set the default startup level, etc. It can also manage dependencies between services, customize service configurations, and manage service logs.

1.3 The difference between systemctl and other similar commands

Compared with the traditional service command, systemctl is more powerful and flexible, supporting more functions and options. Moreover, systemctl can work closely with other tools and features of systemd to provide a more complete system management solution.

Basic usage

2.1 systemctl syntax and common options

To use the systemctl command, you can follow the following syntax:

systemctl [options] [command] [service name]

Common options include:

  • -h: Display help information
  • -t TYPE: Specify the service type, such as service, socket, device, etc.

2.2 Starting and stopping services

Start a service:

systemctl start service name

Stop a service:

systemctl stop service name

2.3 Restart and reload the service

Restart a service:

systemctl restart service name

Reload a service’s configuration file:

systemctl reload service name

2.4 Check service status

Check the running status, PID and other information of a service:

systemctl status service name

2.5 Set default startup level

Set a service to start automatically at boot:

systemctl enable service name

To cancel the automatic startup of a service at boot:

systemctl disable service name

Service management

3.1 List all enabled services

List all enabled services:

systemctl list-unit-files --state=enabled

3.2 Enabling and disabling services

Enable a service:

systemctl enable service name

Disable a service:

systemctl disable service name

3.3 Adding and removing services

Add a custom service (the corresponding unit file needs to be created):

systemctl link /path/to/service unit file

Remove a link to a service:

systemctl unlink /path/to/service unit file

3.4 Customized service configuration

Edit a service’s configuration file:

systemctl edit service name

Then make changes in the editor, save and exit.

Dependency management

4.1 Dependencies between services

View a service’s dependencies:

systemctl show service name --property=Requires
systemctl show service name --property=Wants

4.2 Solving dependency issues

Enabling and disabling services while resolving dependency issues:

systemctl enable --now service name

systemctl disable --ignore-dependencies service name

4.3 Configure service startup sequence

Use Before and After to configure the startup sequence between services:

[Unit]
Before=service1.service
After=service2.service

Log management

5.1 View service log

View the logs of a service:

journalctl -u service name

5.2 Set log level and storage location

Edit the journald.conf file to set the log level and storage location:

sudo systemctl edit --full systemd-journald.service

5.3 Log rotation and compression

Configuring log rotation and compression policies can be accomplished by editing the corresponding unit files.

Common techniques and examples

6.1 Using wildcard batch operation services

systemctl start service name*
systemctl stop service name*
systemctl restart service name*

6.2 Start and stop services regularly

Use timer unit files to start and stop services regularly, similar to cron tasks.

6.3 Customize services through systemd unit files

Create customized service unit files to implement specific needs.

6.4 Process Control and Resource Limitation

Control the service process and limit resources by configuring the relevant parameters of the [Service] section.

Troubleshooting and FAQ

7.1 Find and solve the reasons for service startup failure

Check the service log and use the journalctl command to locate the problem.

7.2 Solve the problem that the service cannot be stopped or reloaded

Try using the systemctl kill command to force stop or reload the service.

7.3 Handling log overflow and write errors

Adjust the log storage location, set the log rotation policy, or increase the system log storage space.

Summary

Through the introduction of this article, we have learned about the systemctl command and its role and use in service management. Compared with other similar commands, systemctl has more powerful and flexible functions.

In the basic usage section, we learned the syntax and common options of the systemctl command, and mastered how to start, stop, restart, reload the service, and view the service status. We also learned how to set the default startup level to automatically start required services.

In the Services Management section, we learned to list all enabled services and enable, disable, add, or remove services as needed. Additionally, we learned how to customize service configurations to meet specific needs.

Dependency management is an important part of system management. In this article we explore the dependencies between services, and how to solve dependency problems and configure the service startup sequence.

Of course, in terms of log management, we learned how to view service logs, set log levels and storage locations, and applied log rotation and compression techniques to improve the efficiency of log management.

In order to better cope with actual situations, this article also introduces some common techniques and examples, such as using wildcards to operate services in batches, starting and stopping services regularly, as well as customizing services and performing process control and resource restrictions through systemd unit files.

Finally, we discuss troubleshooting and common issues. Learn how to find and resolve the reasons why a service fails to start, as well as deal with the inability to stop or reload a service, and how to handle situations such as log overflows and write errors.

By mastering the systemctl command and its various applications, we can manage and maintain system services more efficiently and improve the stability and reliability of system operation.

Python boutique column recommendations

Basic knowledge of python (0 basic introduction)

[Basic knowledge of python] 0.print() function
[Basic knowledge of python] 1. Data types, data applications, data conversion
[Basic knowledge of python] 2.if conditional judgment and conditional nesting
[Basic knowledge of python] 3.input() function
[Basic knowledge of python] 4. Lists and dictionaries
[Basic knowledge of python] 5. for loop and while loop
[Basic knowledge of python] 6. Boolean values and four statements (break, continue, pass, else)
[Basic knowledge of python] 7. Practical operation-Using Python to implement the “Text PK” game (1)
[Basic knowledge of python] 7. Practical operation-Using Python to implement the “Text PK” game (2)
[Basic Knowledge of Python] 8. Programming Thinking: How to Solve Problems – Thinking
[Basic knowledge of python] 9. Definition and calling of functions
[Basic knowledge of python] 10. Writing programs with functions – Practical operation
[Basic knowledge of python] 10. Use Python to implement the rock-paper-scissors game-Function Practical Operation
[Basic knowledge of python] 11. How to debug – Common error reasons and troubleshooting ideas – Thinking
[Basic knowledge of python] 12. Classes and objects (1)
[Basic knowledge of python] 12. Classes and objects (2)
[Basic knowledge of python] 13. Classes and objects (3)
[Basic Knowledge of Python] 13. Classes and Objects (4)
[Basic knowledge of python] 14. Construction of library management system (practical operation of classes and objects)
[Python basics] 15. Coding basics
[Basic knowledge of python] 16. Basics and operations of file reading and writing
[Basic Knowledge of Python] 16. Python Implementation of “Ancient Poetry Dictation Questions” (File Reading, Writing and Coding – Practical Operation)
[Basic knowledge of python] 17. The concept of modules and how to introduce them
[Basic knowledge of python] 18. Practical operation-using python to automatically send mass emails
[Basic knowledge of python] 19. Product thinking and the use of flow charts – Thinking
[Basic Knowledge of Python] 20. Python Implementation of “What to Eat for Lunch” (Product Thinking – Practical Operation)
[Basic knowledge of python] 21. The correct way to open efficiently and lazily – graduation chapter
[Python file processing] Reading, processing and writing of CSV files
[python file processing] Excel automatic processing (using openpyxl)
[python file processing]-excel format processing

Python crawler knowledge

[python crawler] 1. Basic knowledge of crawlers
[python crawler] 2. Basic knowledge of web pages
[python crawler] 3. First experience with crawlers (BeautifulSoup analysis)
[python crawler] 4. Practical crawler operation (dish crawling)
[python crawler] 5. Practical crawler operation (lyrics crawling)
[python crawler] 6. Practical crawler operation (requesting data with parameters)
[python crawler] 7. Where is the crawled data stored?
[python crawler] 8. Review the past and learn the new
[python crawler] 9. Log in with cookies (cookies)
[python crawler] 10. Command the browser to work automatically (selenium)
[python crawler] 11. Let the crawler report to you on time
[python crawler] 12. Build your crawler army
[python crawler] 13. What to eat without getting fat (crawler practical exercise)
[python crawler] 14. Scrapy framework explanation
[python crawler] 15.Scrapy framework practice (crawling popular positions)
[python crawler] 16. Summary and review of crawler knowledge points