SYSTEMD---Linux initialization system

Source: Internet
Author: User

I. Overview

SYSTEMD is a basic building block for a Linux system. It provides a system and service manager that runs as a PID 1 and starts the rest of the system. SYSTEMD provides aggressive parallelization, uses sockets and D-bus activation to start services, provides on-demand boot of daemons, tracks processes using the Linux control group, and maintains mount and AutoMount points (autofs). The SYSTEMD supports SYS V and LSB init scripts and acts as an alternative to SYS v init. Other sections include log daemons, utilities to control basic system configuration, such as hostname, date, locale, maintain list of logged-in users, and run containers and virtual machines, system accounts, runtime catalogs and settings, and daemons that manage simple network configurations. In short, Systemd was born to be able to start the service concurrently, speed up the system startup speed, and monitor the tracking process through the Linux cgroup.

(SYSTEMD architecture diagram, as can be seen from the figure, Systemd took advantage of the Linux three modules cgroups, AutoFS, Kdbus. Optimize the start-up service for Process Control, file Auto-mount, process Communication Service, respectively)

Second, the systemd commonly used commands

1. Systemctl

Commands for operating the control system:

$ systemctl Poweroff # shutdown
$ systemctl Reboot # re-boot
$ systemctl Suspend # Enter pause mode
$ systemctl Hibernate # into sleep mode
$ systemctl Rescue # forced into rescue mode
$ systemctl Emergency # forced into emergency rescue mode

managing a single unitSYSTEMCTL provides a set of subcommands to manage a single unit with a command format of: systemctl [command] [unit] command: Start: Starts immediately after the unit is connected. STOP: Close the unit immediately after. Restart: Immediately after shutting down the unit after the start, that is to do stop and start the meaning. Reload: To reload the configuration file without shutting down the unit, let the settings take effect. Enable: Set the next time the unit will be switched on. Disable: Set the next power on, the unit that follows will not be started. Status: The state of the unit that is currently followed will list information such as whether it is executing or not starting. is-active: There is currently no running in. is-enable: The unit is enabled at startup with no default.  kill : sends a signal to the process that is running the unit. Show: Lists the configuration of the unit. Mask: Log off the unit and you will not be able to start the unit after logging off. Unmask: Cancels the logoff of the unit. 
View unit systemctl on the systemprovides subcommands to view the unit on the system, with the following command format: systemctl [command] [--type=type] [--All] command There are:list-units: Lists the unit that is currently started, if the Add-alloption will also list the unit that is not started. List-unit-files: lists all the unit according to the files in the/lib/systemd/system/directory. --type=type: You can filter a unit of a certain type.
format of the target Unit command:systemctl [command] [unit.target] command  has: Get-default: Gets the current target. Set-default: Sets the following target to be the default mode of operation. Isolate: Switch to the next mode. 
view dependencies between the unit:systemctl list-dependencies [unit] [--reverse] option--reverse will reverse track who is using this unit. to view the system open socket File command:$systemctl list-sockets

2.Systemd-analyze

$ systemd-analyze       #查看系统启动耗时                                                                                $ systemd-analyze Blame  # View startup time per service $ systemd-analyze Critical-chain [Service] #查看系统启动流或者指定服务的启动流

3.Hostnamectl

View current host information, or set the name of the native or deployment environment

4. timedatectl View or set the current time zone

5.loginctl View current logged on user information

iii. SYSTEMD Configuration Service Rules (copy from: http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html, Official document: HTTPS ://www.freedesktop.org/software/systemd/man/systemd.unit.html)

The [Unit] block is usually the first chunk of the configuration file, which defines the unit's metadata and configures the relationship with the other unit. Its main fields are as follows.
Description: Short Description
Documentation: Document Address
Requires: The other unit on which the unit is currently dependent, if they are not running, the current unit will fail to start
Wants: The other unit that mates with the current unit, if they are not running, the current unit does not start failing
Bindsto: Similar to requires, the unit specified by it exits, causing the current unit to stop running
Before: If the unit specified in this field is to be started, it must be started after the current unit
After: If the unit specified by the field is to be started, it must be started before the current unit
Conflicts: The unit specified here cannot be run concurrently with the current unit
Condition ... : The current Unit operation must meet the conditions, otherwise it will not run
Assert... : The current Unit operation must meet the conditions, otherwise it will be reported to start failure


[Install] is usually the last chunk of a configuration file that defines how to start and whether to boot. Its main fields are as follows.
Wantedby: Its value is one or more target, and the current Unit activation (enable) symbolic link is placed under the/etc/systemd/system directory in a subdirectory consisting of the target name +. wants suffix
Requiredby: Its value is one or more target, when the current Unit is activated, the symbolic link is placed in a subdirectory of the/etc/systemd/system directory under the Target name +. Required suffix
Alias: The current Unit can be used to start aliases
Also: When the current unit is active (enable), the other unit that is activated at the same time


The [service] block is used for service configuration and only the service type Unit has this chunk. Its main fields are as follows.
Type: Defines the process behavior at startup. It has the following values.
Type=simple: Default value, execute execstart specified command, start main process
Type=forking: Fork to create child process from parent process, create stepfather process exits immediately
Type=oneshot: One-time process, SYSTEMD will wait for the current service to exit, and then continue to execute down
Type=dbus: Current service starts through D-bus
Type=notify: The current service starts, will notify the SYSTEMD, and then continue to execute down
Type=idle: The current service will run if another task is completed
Execstart: command to start the current service
Execstartpre: Command to execute before starting the current service
Execstartpost: Command executed after starting the current service
Execreload: Commands executed when restarting the current service
Execstop: Command executed when stopping the current service
Execstoppost: Stop a command that executes after its service
RESTARTSEC: Automatically restarts the number of seconds in the current service interval
Restart: Defines what happens SYSTEMD automatically restarts the current service, and the possible values include always (never reboot), on-success, On-failure, On-abnormal, On-abort, On-watchdog
TIMEOUTSEC: Defines the number of seconds to wait before Systemd stops the current service
Environment: Specifying environment variables

Sometimes we need to assemble multiple unit together into a target service. When a target is started, all the unit under this target is started at the same time. Multiple target services can also be started at the same time.

Iv. Log Management commands
# View all logs (by default, only the log for this startup is saved) $sudojournalctl# View kernel log (does not show Application log) $sudoJournalctl-k# Check the system for this startup log $sudoJournalctl-b$sudoJournalctl-b-0# View the last log that was started (you need to change the settings) $sudoJournalctl-b-1# View logs for a specified time $sudoJournalctl--since="2012-10-30 18:17:16"$ sudoJournalctl--since"min ago"$ sudoJournalctl--since yesterday$sudoJournalctl--since"2015-01-10"--until "2015-01-11 03:00"$ sudoJournalctl--since the:xx--until "1 hour ago"# shows the last 10 lines of the tail of the log $sudoJournalctl-n# Displays the tail of the specified number of rows for the log $sudoJournalctl-n -# Real-time scrolling shows the latest logs $sudoJournalctl-F # View logs for a specified service $sudojournalctl/usr/lib/systemd/systemd# View the log of a specified process $sudoJournalctl _pid=1# View a log of the script for a path $sudojournalctl/usr/bin/bash# View the log for a specified usersudoJournalctl _uid= ---since today# view a Unit's log $sudoJournalctl-u nginx.service$sudoJournalctl-u Nginx.service--since today# real-time scrolling shows a Unit's latest log $sudoJournalctl-u Nginx.service-F # Merge displays logs for multiple Unit $ journalctl-U nginx.service-u Php-fpm.service--since today# view logs for the specified priority (and above levels), total 8 levels #0: emerg#1: alert#2: crit#3: err#4: warning#5: notice#6:Info# 7: debug$sudoJournalctl-p Err-b# Log default paging output,--no-Pager change to normal standard output $sudoJournalctl--no-pager# output in JSON format (single line) $sudoJournalctl-b-U Nginx.service-o json# output in JSON format (multiple lines), better readabilitysudoJournalctl-b-u nginx.serviceqq-O json-pretty# Show log occupied disk space $sudoJournalctl--disk-usage# Specifies the maximum space occupied by the log file $sudoJournalctl--vacuum-size=1g# Specify how long the log file is savedsudoJournalctl--vacuum- Time=1years

SYSTEMD---Linux initialization system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.