Centos7 systemd

Centos7 systemd_第1张图片
Centos7 systemd 基础学习

系统开机启动流程:

POST--> boot sequence --> bootloader --> kernel + initramfs(initrd)--> rootfs  --> /sbin/init
# 又想起了加载kernel时,kernel是在文件系统中,所以还是需要先挂载文件系统,所以还是需要驱动,挂载文件系统的驱动在哪里呢?

init:

centos 5: sysv init
centos 6: upstart
centos 7: systemd   向后不完全兼容sysv init,upstart

systemd新特性:

> 系统引导时,实现服务并行启动
> 按需激活进程
> 系统状态快照
> 基于依赖关系定义服务控制逻辑

核心概念: unit(单元)

> 配置文件进行标识和配置,文件中主要包含了系统服务,监听socket,保存系统快照以及其他与init相关信息。
> 保存位置:(常用位置)
    > /usr/lib/systemd/system
    > /run/systemd/system
    > /etc/systemd/system

Unit类型:

> Service unit: 文件扩展名为.service 用于定义系统服务
> Target unit: 文件扩展名为.target,用于模拟实现“运行级别”,但并不完全是。
> Device unit: .device 用于定义内核识别的设备
> Mount unit: .mount,定义文件系统挂载点
> socket unit: .socket,用于标识进程间通信用的socket文件
> snapshot unit: .snapshot,管理系统快照
> Swap unit: .swap,用于标识swap设备
> Automount unit: .automount,文件系统的自动挂载点
> Path unit: .path,用于定义文件系统中的一个文件或目录

关键特性:

基于socket的激活机制: socket与服务程序分离,当有进程需要访问此socket才真正启动
基于bus的激活机制
基于device的激活机制:类似插入u盘,需要激活mount服务来挂载设备
基于path的激活机制:监控文件路径或目录发生变化,立即激活某服务
系统快照:将unit当前状态数据保存在持久设备中
向后兼容sysv init脚本

不兼容:

systemctl命令固定不变,不像sysv init脚本可以自由定义。
非由systemd启动的服务,systemctl无法与之通信。
5分钟超时时间。

在Centos 5,6当中运行,级别切换过程(运行级别2切换到运行级别3):将级别3的k服务全部kill掉,s服务全部start

管理系统服务命令帮助:

Centos 7 : service unit,能兼容早期的服务脚本
命令: systemctl COMMAND  NAME.service
Unit Commands:
  list-units [PATTERN...]         List loaded units    (列出系统已经load的unit,-a查看所有的unit ,-t service查看service类型)
  list-sockets [PATTERN...]       List loaded sockets ordered by address   (列出系统已经加载socket类型unit)
  list-timers [PATTERN...]        List loaded timers ordered by next elapse
  start NAME...                   Start (activate) one or more units   (启动服务)
  stop NAME...                    Stop (deactivate) one or more units  (停止服务)
  reload NAME...                  Reload one or more units             (重载服务)
  restart NAME...                 Start or restart one or more units   (重启服务)
  try-restart NAME...             Restart one or more units if active
  reload-or-restart NAME...       Reload one or more units if possible,
                                  otherwise start or restart
  reload-or-try-restart NAME...   Reload one or more units if possible,
                                  otherwise restart if active
  isolate NAME                    Start one unit and stop all others   (切换运行级别)
  kill NAME...                    Send signal to processes of a unit   (向某unit进程发送信号)
  is-active PATTERN...            Check whether units are active
  is-failed PATTERN...            Check whether units are failed
  status [PATTERN...|PID...]      Show runtime status of one or more units
  show [PATTERN...|JOB...]        Show properties of one or more
                                  units/jobs or the manager
  cat PATTERN...                  Show files and drop-ins of one or more units
  set-property NAME ASSIGNMENT... Sets one or more properties of a unit
  help PATTERN...|PID...          Show manual for one or more units
  reset-failed [PATTERN...]       Reset failed state for all, one, or more
                                  units
  list-dependencies [NAME]        Recursively show units which are required
                                  or wanted by this unit or by which this
                                  unit is required or wanted

Unit File Commands:
  list-unit-files [PATTERN...]    List installed unit files  (列出unit服务开机启动状态,-t service 仅查看service类型的服务)
  enable NAME...                  Enable one or more unit files  (开机启动)
  disable NAME...                 Disable one or more unit files  (禁止开机启动)
  reenable NAME...                Reenable one or more unit files
  preset NAME...                  Enable/disable one or more unit files
                                  based on preset configuration
  preset-all                      Enable/disable all unit files based on
                                  preset configuration
  is-enabled NAME...              Check whether unit files are enabled  (查看服务是否开机自启动)
  mask NAME...                    Mask one or more units
  unmask NAME...                  Unmask one or more units
  link PATH...                    Link one or more units files into
                                  the search path
  add-wants TARGET NAME...        Add 'Wants' dependency for the target
                                  on specified one or more units
  add-requires TARGET NAME...     Add 'Requires' dependency for the target
                                  on specified one or more units
  edit NAME...                    Edit one or more unit files
  get-default                     Get the name of the default target   (查看默认允许级别)
  set-default NAME                Set the default target    (设置默认运行级别)

Machine Commands:
  list-machines [PATTERN...]      List local containers and host

Job Commands:
  list-jobs [PATTERN...]          List jobs
  cancel [JOB...]                 Cancel all, one, or more jobs

Snapshot Commands:
  snapshot [NAME]                 Create a snapshot    (创建unit快照)
  delete NAME...                  Remove one or more snapshots    (删除unit快照)

Environment Commands:
  show-environment                Dump environment            (查看运行环境,如语言,搜索路径)
  set-environment NAME=VALUE...   Set one or more environment variables
  unset-environment NAME...       Unset one or more environment variables
  import-environment [NAME...]    Import all or some environment variables

Manager Lifecycle Commands:
  daemon-reload                   Reload systemd manager configuration
  daemon-reexec                   Reexecute systemd manager

System Commands:
  is-system-running               Check whether system is fully running
  default                         Enter system default mode            
  rescue                          Enter system rescue mode             (紧急救援模式)
  emergency                       Enter system emergency mode          (emergency模式,比rescue模式更彻底)
  halt                            Shut down and halt the system        (关机)
  poweroff                        Shut down and power-off the system   (关机)
  reboot [ARG]                    Shut down and reboot the system      (重启)
  kexec                           Shut down and reboot the system with kexec
  exit                            Request user instance exit
  switch-root ROOT [INIT]         Change to a different root file system   (切换根文件系统)
  suspend                         Suspend the system     (挂起系统)
  hibernate                       Hibernate the system  (保存系统快照)
  hybrid-sleep                    Hibernate and suspend the system    (保存系统快照并挂起)

运行级别:

runlevel0.target -> poweroff.target
runlevel1.target -> rescue.target
runlevel2.target -> multi-user.target
runlevel3.target -> multi-user.target
runlevel4.target -> multi-user.target
runlevel5.target -> graphical.target
runlevel6.target -> reboot.target

运行级别切换:systemctl isolate graphical.target
不能使用runlevel切换运行级别,可以使用runlevel查看

切换至紧急求援模式:

systemctl rescue

切换至emergency模式:

systemctl emergency

设置服务开机自启动:

systemctl enbale nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 只是在/etc/systemd/system/multi-user.target.wants/目录下做了一个软链接
# 禁止开机自启动也只是将此目录下的服务名称软链接删除

类似的还有设置默认运行级别:

# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

思考:这样的设计思想与git在分支管理,版本管理上是否有异曲同工之妙?

内容按需快照一份,设计一个head指针,将指针指向我们需要的内容位置即可,高效,灵活,易理解。
我们人的记忆不也类似这样的一个工作方式,将记忆碎片重现。

unit编写:

[Unit]
Description=The nginx HTTP and reverse proxy server        描述
After=network.target remote-fs.target nss-lookup.target    服务先后次序与依赖关系

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid    start前,先删除是否有pid文件
ExecStartPre=/usr/sbin/nginx -t      start前,先测试配置文件准确性
ExecStart=/usr/sbin/nginx      启动nginx  
ExecReload=/bin/kill -s HUP $MAINPID   重启,即是向nginx进程发送HUP信号
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target     多用户模式启动

# unit 单元文件编写:
# https://www.freedesktop.org/software/systemd/man/systemd.unit.html、
# http://blog.csdn.net/yuesichiu/article/details/51331136#t0

你可能感兴趣的:(Centos7 systemd)