Linux service management command service and systemctl

Article directory

  • One: Historical background
  • Two: service command
  • Three: systemctl command
    • service management
    • systemd target
    • log function

One: Historical Background

Linux system services, sometimes called daemons, are system tasks that are automatically loaded when Linux starts and stopped when Linux exits. The operation of linux on services in historical versions is done through service. If you create a user-defined service, more complicated operations are required. At present, the new release version of linux has built-in systemctl to operate the service.

In early service management, Init is the first process created when the Linux system starts. It is a daemon process that runs until the system is shut down. init is the direct or indirect ancestor of all other processes and automatically guardianships all orphaned processes. The kernel starts it according to the hard-coded filename, and if the kernel fails to start it, it will cause a kernel panic. The process identifier (PID) of init is usually 1. During system startup and shutdown, the init process starts init scripts (or rc) to ensure basic functionality. This includes mounting and unmounting filesystems, and starting daemons. Further, there is a service manager that provides active control over started processes, called process monitoring. For example, monitor crashed processes and restart them when appropriate. These elements add up to the init system. Some init include the service manager in the init process, or have a closely related init script. In the following, this type of init will be referred to as integrated. Items in other categories may depend on each other.

Currently, many Linux distributions have switched to systemd. systemd is a collection of basic components of the Linux system, providing a system and service manager, running as PID 1 and responsible for starting other programs. Features include: support for parallel tasks; use both socket and D-Bus bus activation services; start daemons on demand; use Linux cgroups (Simplified Chinese) to monitor processes; support snapshots and system recovery; maintain mounts Points and automatic mount points; precise control between services based on dependencies. systemd supports SysV and LSB init scripts and can be used as an alternative to sysvinit. In addition, the functions also include log process, control basic system configuration, maintain login user list and system account, runtime directory and settings, can run containers and virtual machines, and can easily manage network configuration, network time synchronization, and log forwarding and name resolution etc.

The Init system is a collection of simple and small services. It is complicated to configure, but the configuration process is transparent. However, systemd is a huge system with many functions and is easy to configure. There are different opinions about this conversion. Some people like its powerful functions and simple operation, while others criticize it for not conforming to the Unix philosophy.

Two: service command

service is a command for service management under the Init system. The service command itself is a shell script. It searches for a specified service script in the /etc/init.d/ directory, and then calls the service script to complete the task.

The service can be managed through the sercice SCRIPT COMMAND command, and SCRIPT is an executable script file stored in /etc/init.d/:

service SCRIPT start #Start service
service SCRIPT stop #stop service
service SCRIPT restart #Restart service
service SCRIPT status #View status service

In addition, service –status-all can be used to display a list of all system services, where “+” means that the service is running, and “-” means that the service is closed, and “?” means that there is no status at all.< /strong>.
To view running services, you can also use ps aux | grep service_name to view the process status; if it is a network service, you can also view the monitoring status of the port, execute netstat -tuln | grep service_name/port_number, for example, you can execute netstat -tuln | grep ftp to view the port status , the default port is 21.
Users can add custom services and place corresponding scripts under the /etc/init.d/ folder.

Three: systemctl command

Service Management

systemctl is a service management command under the systemd system. It contains many functions, see systemctl for details

systemctl status #Display system status
systemctl start [unit] #Activate the unit immediately:
systemctl stop [unit] #stop the unit immediately
systemctl restart [unit] #restart unit
systemctl enable [unit] #Automatically activate the unit at startup
systemctl disable [unit] #Cancel the automatic activation of the unit after booting
systemctl daemon-reload #Reload systemd, scan for new or changed units

Systemd uses the “Unit” monitoring management system, and the unit file is an ini-style plain text file. Encapsulates information about the following objects: service (service), socket (socket), device (device), mount point (mount), automount point (automount), boot target (target), swap partition or swap File (swap), monitored path (path), task plan (timer), resource control group (slice), and a set of externally created processes (scope). For the detailed introduction of the unit, please refer to the system.unit Chinese manual
A service that can be managed by systemctl needs a .service file. In Ubuntu, this file is usually located in the /etc/systemd/system folder. For the Systemd service unit, refer to the systemd.service Chinese manual.
After you have changed the relevant service configuration file, you need to run the following command to restart the service:

sudo systemctl daemon-reload & amp; & amp; sudo systemctl restart name
# where name is the service you want to restart

systemd target

The concept of targets in Systemd, systemd targets are represented by target units. Target unit files end with the .target file extension, and their sole purpose is to group other systemd units together through a dependency chain. For example, the graphical.target unit for starting a graphical session starts system services such as the GNOME Display Manager (gdm.service) or Accounts Service (accounts-daemon.service), and also activates the multi-user.target unit. Likewise, the multi-user.target unit starts other basic system services such as NetworkManager (NetworkManager.service) or D-Bus (dbus.service), and activates another target unit called basic.target. This mode replaces the predefined runlevels in SysV init.

SysV runlevels compared to systemd targets

Runlevel Target unit Description
0 runlevel0.target, poweroff.target Shutdown system
1 runlevel1.target, rescue.target set rescue shell
2 runlevel2.target, multi-user.target Setting up a non-graphical multi-user system
3 runlevel3.target , multi-user.target Set up a non-graphical multi-user system
4 runlevel4.target, multi-user.target Set up a non-graphical multi-user system
5 runlevel5.target, graphical.target Set up a graphical multi-user system
6 runlevel6.target, reboot.target Shut down and reboot the system

Order:

# sysvinit runlevel
runlevel #View the current run level
init N #Enter other runlevels

# systemd target
systemctl list-units --type=target #Lists the currently loaded and activated targets
systemctl isolate graphical.target #switch target

Log function

In addition, systemd provides a log function, and viewing logs is an important way for us to find problems. journalctl is used to query logs collected by the systemd-journald service. The systemd-journald service is a service provided by the systemd init system to collect system logs`

The commonly used commands for the log function are as follows:

journalctl #Without any options, output all log records by default
journalctl -n [num] #Display the log of the last num lines, if num is omitted, the last 10 lines will be displayed by default
journalctl -f #Real-time scrolling display of the latest log
journalctl -u [unit] #Display the log of the specified unit

Viewing logs requires super administrator privileges.

# Specific usage see
journalctl -h
# For example, view the latest log of the docker service
sudo journalctl -u docker | tail -n 50

syntaxbug.com © 2021 All Rights Reserved.