Linux boot management --- systemd uses

  
to modify the configuration file and restart

After modifying the configuration file, you need to reload the configuration file, and then restart the related services.

# Reload configuration file $ sudo systemctl daemon-reload# Restart related services $ sudo systemctl restart foobar Boot service management 

Boot start unit

systemctl enable test.service increase Soft link ln -s '/usr/lib/systemd/system/postfix.service' from /lib/systemd/system/to /etc/systemd/system/multi-user.target.wants/'/etc/systemd /system/multi-user.target.wants/test.service' 

Once the configuration file is modified, let SystemD reload the configuration file and restart it, otherwise the modification will not take effect.

$ sudo systemctl daemon-reload$ sudo systemctl restart httpd.service 

boot does not start unit

systemctl disable test.service delete /etc/systemd/system/multi-user.target Soft link under .wants 

Check if boot is enabled

systemctl is-enabled test.service #Query service is booting 

systemd view boot self-starting program

ls /Etc/systemd/system/multi-user.target.wants/

View systemd unit loading and activity

systemctl 

Display unit failed to start

systemctl --failed 

View all units managed by systemd

systemctl list-unit-files Service Management

Start Service

systemctl start httpd.service 

Close Service
< Pre>systemctl stop httpd.service

Restart Service

systemctl restart httpd.service 

Reload

systemctl reload httpd.service 

View Status

 Systemctl status httpd.service includes startup status, startup time, main process and related processes, related logs systemctl

s Ystemctl is Systemd's main command for managing systems.

# Restart the system $ sudo systemctl reboot# Shut down the system, cut off the power $ sudo systemctl poweroff # CPU stop working $ sudo systemctl halt # Suspend system $ sudo systemctl suspend # Let the system enter hibernation state $ sudo systemctl hibernate# let The system enters interactive hibernation state. sudo systemctl hybrid-sleep# Starts into rescue state (single-user state) $ sudo systemctl rescue systemctl list-units command to view all units of the current system. 
# List the running Unit$ systemctl list-units# List all Units, including $systemctl list-units not found in the configuration file or failed to start --all# List all units that are not running $ systemctl list-units --all --state=inactive# List all Unit#l.ctctlist list-units failed to load --failed# List all running units of type $systemctl list-units --type =service
# Shows whether a Unit is running $ systemctl is-active application.service# Shows whether a Unit is in a startup failure state $ systemctl is-failed application.service# Shows whether a Unit service has a startup link established $ systemctl is-enabled application.service configuration block 

[Unit] block is usually the first block of the configuration file, used to define the metadata of the Unit, and configure the relationship with other Units . Its main fields are as follows.

Description: Short description Documentation: Document address Requires: Other Units that the current Unit depends on. If they are not running, the current Unit will fail to start. Wants: Other units that match the current Unit, if they are not running, the current Unit Does not start failure BindsTo: Similar to Requires, if the specified unit exits, it will cause the current Unit to stop running Before: If the specified unit of the field is also started, then the After must be started after the current Unit: If the specified unit of the field To start, then you must start Conflicts before the current Unit: The Unit specified here cannot run Condition with the current Unit: the condition that the current Unit must run, otherwise it will not run Assert: the condition that the current Unit must meet, otherwise it will report Startup failed 

!!! Important

eg:After=network.target sshd-keygen.serviceAfter field: indicates that if network.target or sshd-keygen.service needs to be started, then the current .service should be in them Then start. Accordingly, there is a Before field that defines which services sshd.service should be started before. Note that the After and Before fields only refer to the startup sequence and do not involve dependencies. 

[Install] is usually the last block of the configuration file to define how to boot and whether to boot. Its main fields are as follows.

WantedBy: Its value is one or more Targets. The current Unit activation (enable) symbolic link will be placed in the /etc/systemd/system directory under the subdirectory of the Target name + .wants suffix. RequiredBy: Its value is one or more Targets. When the current Unit is activated, the symbolic link will be placed in the /etc/systemd/system directory under the Target name + .required suffix in the subdirectory. Alias: The current Unit can be used to start. Alias: When the current Unit is activated, other Unit 

[Service] blocks that are activated at the same time are used for Service configuration. Only Units of Service type have this block. Its main fields are as follows.

Type: Defines the behavior of the process at startup. It has the following values. Type=simple: default value, execute the command specified by ExecStart, start the main process Type=forking: create a child process from the parent process in the fork mode. After the creation, the parent process will immediately exit Type=oneshot: one-time process, Systemd will wait for the current service. Exit, and then continue to execute Type=dbus: The current service starts Type=notify through D-Bus: After the current service is started, Systemd will be notified, and then continue to execute Type=idle: If other tasks are executed, the current service is completed. ExecStart will be run: Command to start the current service ExecStartPre: Command ExecStartPost executed before starting the current service: ExecReload executed after starting the current service: ExecStop executed when restarting the current service ExecStop: ExecStopPost executed when the current service is stopped: Stop when The command executed after the service RestartSec: the number of seconds to automatically restart the current service interval. Restart: Defines the situation. Systemd will automatically restart the current service. Possible values ​​include always (always restart), on-success, on-failure, on-abnormal , on-abort, on-watchdogTimeoutSec: define Systemd Wait before stopping the current service in seconds Environment: Specifies the environment variable 

full field list Unit configuration file, please refer to the official documentation.
Log Management

Systemd manages the startup log of all Units in a unified manner. The benefit is that you can view all logs (kernel logs and application logs) with just one command of journalctl. The configuration file for the log is /etc/systemd/journald.conf.

Copyright © Windows knowledge All Rights Reserved