服务控制
1.systemd
1.1 systemd简介
- systemd是用户空间的第一个应用程序,即/sbin/init
- init程序的类型
- SysV风格:init(centos5版本),实现系统初始化时,随后的初始化操作都是借助于脚本来实现的
- 特点:
- 脚本中含有大量的命令,每个命令都要启动一个进程,命令执行完以后就需要终止这个进程。如此一来,系统初始化时将大量的创建进程,销毁进程,工作效率非常低。
- 服务之间可能会存在依赖关系,必须严格按照一定的顺序来启动服务,前一个服务没启动后面的服务就无法执行启动过程。
不能并行进行。
- 配置文件:
/etc/inittab
- 特点:
- SysV风格:init(centos5版本),实现系统初始化时,随后的初始化操作都是借助于脚本来实现的
- Upstart风格:init(centos6版本),由ubuntu研发,通过总线形式以接近并行的方式工作,效率比SysV高
- 特点:
- 基于总线方式能够让进程间互相通信的一个应用程序。
- 不用等服务启动完成,只要已初始化就可以把自己的状态返回给其他进程
- 配置文件:
/etc/inittab , /etc/init/*.conf
- 特点:
- systemd风格:systemd(centos7)
- 特点:启动速度比sysv和upstart都快
- 不需要通过任何脚本来启动服务,systemd自身就可以启动服务,其本身就是一个强大的解释器,启动服务时不需要sh/bash的参与
- systemd不真正在系统初始化时去启动任何一个服务
- 只要服务没用到,它告诉你启动了,实际上并没有启动。仅当去访问时才会真正启动服务。
- 配置文件:
/usr/lib/systemd/system, /etc/system/system
- 特点:启动速度比sysv和upstart都快
守护进程是在执行各种任务的后台等待或运行的进程。为了侦听连接,守护进程使用套接字(IP+端口号)。
1.2systemd的核心概念unit
systemd
使用unit
的概念来管理服务,这些unit
表现为一个个配置文件。
systemd
通过这些配置文件进行标识和配置达到管理服务的目的:
这些unit文件中主要包含了系统服务、监听socket、保存的系统快照、及其他与init相关的信息保存至以下目录:
/usr/lib/systemd/system
/run/systemd/system
/etc/systemd/system
unit的类型
- service unit :文件扩展名为.service,用于定义系统服务
- Target unit :文件扩展名为.target,用于模拟实现“运行级别”
- runlevel0.target和poweroff.target :关机
- runlevel1.target和rescue.target:单用户模式
- runlevel2.target和multi-user.target:对于systemd来说,2/3/4级别没有区别
- runlevel3.target和multi-user.target:对于systemd来说,2/3/4级别没有区别
- runlevel4.target和multi-user.target:对于systemd来说,2/3/4级别没有区别
- runlevel5.target和graphical.target:图形级别
- runlevel6.target和reboot.target:重启
- Device unit :文件扩展名为.device,用于定义内核识别的设备
- Mount unit :文件扩展名为.mount,用于定义文件系统挂载点
- Socket unit :文件扩展名为.socket,用于标识进程间通信用的socket文件
- Snapshot unit :文件扩展名为.snapshot,用于管理系统快照
- Swap unit :文件扩展名为.swap,用于标识swap设备
- Automount unit :文件扩展名为automount,用于实现文件系统的自动挂载点
- Path unit :文件扩展名为.path,用于定义文件系统中的一个文件或目录
UNIT关键特性
- 基于socket的激活机制:
- socket与服务程序分离,当有人去访问时才会真正启动服务,以此来实现按需激活进程与服务的并行启动
- 基于bus的激活机制:
- 所有使用dbus实现进程间通信的服务,可以在第一次被访问时按需激活
- 基于device的激活机制:
- 支持基于device激活的系统服务,可以在特定类型的硬件接入到系统中时,按需激活其所需要用到的服务
- 基于path的激活机制:
- 某个文件路径变得可用,或里面出现新文件时就激活某服务
- 系统快照:
- 保存各unit的当前状态信息于持久存储设备中,必要时能自动载入
- 向后兼容sysv init脚本
不兼容特性
- systemctl命令固定不变
- 非由systemd启动的服务,systemctl无法与之通信
- 只有已经启动的服务在级别切换时才会执行stop,在centos6以前是所有S开头的服务全部start,所有K开头的服务全部stop
- 系统服务不会读取任何来自标准输入的数据流
- 每个服务的unit操作均受5分钟超时时间限制
2.使用systemctl管理服务
语法:systemctl COMMAND name[.service|.target]
-
常用命令:
-
start name.service //启动服务
示例:[root@localhost ~]# systemctl start postfix
-
stop name.service //停止服务
示例:[root@localhost ~]# systemctl stop postfix [root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: inactive (dead) since 三 2019-09-25 17:01:40 CST; 21s ago Process: 1647 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 1195 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 1188 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 1125 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 1347 (code=killed, signal=TERM)
-
restart name.service //重启服务,重启后停止的服务会被启动
示例:[root@localhost ~]# systemctl restart postfix [root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 三 2019-09-25 17:03:43 CST; 11s ago Process: 1647 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 1682 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 1679 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 1676 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 1754 (master) CGroup: /system.slice/postfix.service ├─1754 /usr/libexec/postfix/master -w ├─1755 pickup -l -t unix -u └─1756 qmgr -l -t unix -u
-
status name.service //查看服务状态
示例:[root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 三 2019-09-25 10:25:03 CST; 6h ago Process: 1195 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 1188 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 1125 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 1347 (master) CGroup: /system.slice/postfix.service ├─1347 /usr/libexec/postfix/master -w ├─1349 qmgr -l -t unix -u └─1590 pickup -l -t unix -u
-
try-restart name.service //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
示例:[root@localhost ~]# systemctl try-restart postfix
-
reload-or-restart name.service //重载或重启服务,能reload则reload,否则restart
示例:[root@localhost ~]# systemctl reload-or-restart postfix
-
reload-or-try-restart name.service //重载或条件式重启服务,能reload则reload,否则try-restart
示例:[root@localhost ~]# systemctl reload-or-try-restart postfix
-
mask name.service //禁止设定为开机自启
示例:[root@localhost ~]# systemctl mask postfix Created symlink from /etc/systemd/system/postfix.service to /dev/null. [root@localhost ~]# systemctl status postfix ● postfix.service Loaded: masked (/dev/null; bad) Active: active (running) since 三 2019-09-25 17:07:35 CST; 1h 1min ago
-
unmask name.service //取消禁止设定为开机自启
示例:[root@localhost ~]# systemctl unmask postfix Removed symlink /etc/systemd/system/postfix.service. [root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 三 2019-09-25 17:07:35 CST; 1h 2min ago
-
list-dependencies name.service //查看服务的依赖关系
示例:[root@localhost ~]# systemctl list-dependencies postfix postfix.service ● ├─system.slice ● └─basic.target ● ├─microcode.service ● ├─rhel-autorelabel-mark.service ● ├─rhel-autorelabel.service ● ├─rhel-configure.service ● ├─rhel-dmesg.service ● ├─rhel-loadmodules.service ● ├─selinux-policy-migrate-local-changes@targeted.service ● ├─paths.target ● ├─slices.target ● │ ├─-.slice
-
is-active name.service //查看某服务当前激活与否的状态
示例:[root@localhost ~]# systemctl is-active postfix active
-
is-enabled name.service //查看服务是否开机自动启动
示例:[root@localhost ~]# systemctl is-enabled postfix enabled
-
enable name.service //设定某服务开机自动启动
示例:[root@localhost ~]# systemctl enable postfix [root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since 三 2019-09-25 17:07:35 CST; 51min ago Main PID: 1853 (master) CGroup: /system.slice/postfix.service ├─1853 /usr/libexec/postfix/master -w ├─1892 pickup -l -t unix -u └─1893 qmgr -l -t unix -u
-
disable name.service //禁止服务开机自动启动
示例:[root@localhost ~]# systemctl disable postfix Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service. [root@localhost ~]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
-
isolate name.target //切换至某级别,如systemctl isolate graphical.target就是切换至图形界面
-
list-unit-files --type service //查看所有服务的开机自动启动状态(是否开机自启)
示例:[root@localhost ~]# systemctl list-unit-files --type service UNIT FILE STATE arp-ethers.service disabled auditd.service enabled autovt@.service enabled blk-availability.service disabled brandbot.service static chrony-dnssrv@.service static chrony-wait.service disabled chronyd.service enabled console-getty.service disabled console-shell.service disabled container-getty@.service static cpupower.service disabled crond.service enabled dbus-org.fedoraproject.FirewallD1.service enabled dbus-org.freedesktop.hostname1.service static
-
list-units --type service //查看所有已经激活的服务状态信息
示例:[root@localhost ~]# systemctl list-units --type service UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service chronyd.service loaded active running NTP client/server crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus firewalld.service loaded active running firewalld - dynamic firewall da getty@tty1.service loaded active running Getty on tty1 irqbalance.service loaded active running irqbalance daemon .....
-
list-units --type target //查看所有已装载的级别
示例:[root@localhost ~]# systemctl list-units --type target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System
-
list-units --type service --all //查看所有服务(已启动/已停止)的状态信息
示例:[root@localhost ~]# systemctl list-units --type service --all UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service brandbot.service loaded inactive dead Flexible Branding Service chronyd.service loaded active running NTP client/server cpupower.service loaded inactive dead Configure CPU power relate crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus ● display-manager.service not-found inactive dead display-manager.service dm-event.service loaded inactive dead Device-mapper event daemon dracut-shutdown.service loaded inactive dead Restore /run/initramfs ebtables.service loaded inactive dead Ethernet Bridge Filtering emergency.service loaded inactive dead Emergency Shell ● exim.service not-found inactive dead exim.service
-
list -units --type target --all //查看所有的级别
示例:[root@localhost ~]# systemctl list-units --type target --all UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes emergency.target loaded inactive dead Emergency Mode final.target loaded inactive dead Final Step getty.target loaded active active Login Prompts graphical.target loaded inactive dead Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems
-
get-default //查看默认运行级别
示例:[root@localhost ~]# systemctl get-default multi-user.target
-
set-default name.target //设置默认运行级别
示例:[root@localhost ~]# systemctl set-default multi-user.target
-
rescue //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
示例: -
emergency //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
示例: -
halt //关机
-
poweroff //关机
-
reboot //重启
-
suspend //挂起系统,此时不能关机,否则无用
-
hibernate //创建并保存系统快照,下次系统重启时会自动载入快照
-
hybrid-sleep //混合睡眠,快照并挂起
-
-
chkconfig 主要用来更新(启动或停止)和查询系统服务的运行级信息。chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。
- - -level :指定读系统服务要在哪一个执行等级中开启或关毕。
示例:[root@localhost ~]# chkconfig --level 5 network off [root@localhost ~]# chkconfig --list 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:关 6:关 rhnsd 0:关 1:关 2:开 3:开 4:开 5:开 6:关 [root@localhost ~]# chkconfig --level 5 network on [root@localhost ~]# chkconfig --list 注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关 rhnsd 0:关 1:关 2:开 3:开 4:开 5:开 6:关
- - -level :指定读系统服务要在哪一个执行等级中开启或关毕。