The basic use of systemd

systemctl command

  • format:systemctl <COMMAND> <name.service>

    • .service Can be omitted, the system will auto-complete
  • Profiles

    • Each Unit has a configuration file that tells Systemd how to start this Unit
    • Profiles on the main /usr/lib/systemd/systemdirectory, it may in /etc/systemd/systemdirectory

System Management

# 检查系统上是否安装了systemd
systemd --version

# 重启系统
systemctl reboot

# 显示主机信息
hostnamectl

#显示主机时区
timedatectl

Management Services

  • systemctlCommand enable, disableand maskthe sub-command has been added --nowat the same time option to be enabled at the same time to start the service, stop the service deactivated

  • start up

    • systemctl start name.service
  • stop

    • systemctl stop name.service
  • Restart

    • systemctl restart name.service
  • status

    • systemctl status name.service
  • Reload the configuration file

    • Here's the configuration file refers to the /usr/lib/systemd/systemfiles in the directory
      • Not the program itself profile
    • systemctl reload name.service
  • Setting a service boot from Kai

    • Equivalent to /etc/systemd/systemadding a symbolic link directory, point /usr/lib/systemd/systeminside the name.servicefile
      • When turned on, Systemdonly perform /etc/systemd/systemdirectory profile
    • After setting the boot, the software will not start immediately
    • systemctl enable name.service
  • Cancellation boot from Kai

    • systemctl disable name.service
  • Disable Service

    • Other services to prevent the service is indirect start, the service can not be started by start or restart command
    • systemctl mask name.service
  • Enable Service

    • Only for service to disabled
    • systemctl unmask name.service
  • Kill service and its child processes

    • systemctl kill name.service
  • List all dependent services

    • systemctl list-dependencies name.service

View Service

#查看服务激活与否
systemctl is-active name.service

#查看服务的开机自启状态
systemctl is-enabled name.service

#查看系统所有运行的服务项
systemctl list-units --type=service

journalctl command

  • systemd log configuration file is/etc/systemd/journald.conf
# 需要root权限

# 查看所有日志(默认情况下 ,只保存本次启动的日志)
journalctl
# 查看系统本次启动的日志
journalctl -b
# 显示尾部的最新n行日志;默认为10
journalctl -n 20
# 滚动显示
journalctl -f

# 查看指定服务(unit)的日志
journalctl -u <service name> 
# 根据pid查看服务日志
journalctl -b _PID=xxx

# 查看指定时间的日志
journalctl --since="2012-10-30 18:17:16"
journalctl --since "20 min ago"
journalctl --since yesterday
journalctl --since 09:00 --until "1 hour ago"

Daemon concept

  • Daemon is Daemon program is a program executed in the background, it is independent of the control terminal and perform some tasks or triggering event cycle
    • End is typically designated as "d" letters, such as the httpd , the syslogd , systemd and dockerd etc.
  • By ampersand, you can put the command into the background, but it is different with the daemon
    • Daemon has nothing to do with the terminal, the init process is adopted orphan process; and the parent process background process is a terminal, the terminal can still print
    • Daemon is still strong in the closed terminal; and background process with the user exits and stop, unless coupled nohup
    • Daemon changed the conversation, process group, the working directory and file descriptors, background processes directly inherit the parent process (shell) of

service and systenctl

  • service is the essence of the command to execute /etc/init.d/the script directory
  • systemctl command compatible with the service
  • Command substitution
    • Start : service name start==>systemctl start name.service
    • Stop : service name stop ==>systemctl stop name.service
    • Restart : service name restart ==>systemctl restart name.service
    • Status : service name status ==>systemctl status name.service

syslog与systemd-journal

  • syslog It is a protocol that defines the format of the message
    • Actually run in the system is syslogda daemon
    • It listening socket /dev/loginformation from the socket and is written to a log file
    • As /var/log/messagesor/var/log/syslog
  • systemd-journald is syslog supplement
    • systemd logs stored in /run/log/journalthe system reboot will clear
    • By the new /var/log/journaldirectory, the log will be automatically recorded into this directory, and perpetual

systemd Chart

Here Insert Picture Description

reference

Published 161 original articles · won praise 19 · views 50000 +

Guess you like

Origin blog.csdn.net/winter_wu_1998/article/details/103724466