17.1 什么是daemon与服务(service)
服务是指常驻内存的进程且可以提供一些系统或网络功能,服务的运作需要daemon执行,比如周期性计划任务服务,需要crond这个daemon,一般可以等同
目前系统使用systemd这个启动服务管理机制,将daemon执行脚本都成为服务单元(unit),服务类型如下:
扩展名 | 主要服务功能 |
---|---|
.service | 一般服务类型:主要是系统服务。包括服务器的本地服务和网络服务等 |
.socket | 内部程序数据交换的服务 |
.target | 执行环境类型,其实是一群unit的集合,执行一个target其实就是执行一堆service或(及).socket |
.mount/.automount | 文件系统挂在相关的服务,例如来自网络的自动挂载 |
17.2 通过systemctl管理服务
17.2.1 通过systemctl管理服务
命令格式:systemctl [ command ] [ unit ]
command 主要有:
start
stop
restart
reload:不关闭后面unit情况下,重新加载配置文件,让配置文件生效
enable:设置开机启动
disable:设置开机不启动
status
is-active:目前有没有在运行中
is-enable:开机时有没有默认启动
[root@host-192-168-230-101 tecs]# systemctl status atd.service
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2022-09-16 16:11:39 CST; 3s ago
Process: 13892 ExecStart=/usr/sbin/atd -f $OPTS (code=exited, status=0/SUCCESS)
Main PID: 13892 (code=exited, status=0/SUCCESS)
Sep 16 16:11:39 host-192-168-230-101 systemd[1]: Stopping Job spooling tools...
Sep 16 16:11:39 host-192-168-230-101 systemd[1]: Stopped Job spooling tools.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
重点在二、三行(Loaded和Active),最后几行是日志,如果发生问题,可以查看这里的日志
Loaded:这行在说明,开机的时候这个unit是否会启动,具体状态如下
enabled:开机启动
disabled:不会启动
static:不会自己启动,不过可能被其他的服务唤醒
mask:永远不会被启动,可使用unmask改回
Active:当前的运行状态,具体如下:
active(running):正在运行
active(exited):只执行一次就正常结束
active(waiting):正在运行中,不过要等待其他事件发生才能继续运行
inactive:目前没有运行
tips:某些服务由于存在依赖性,只停止一个服务可能会被其他服务唤醒,需要把依赖的服务也关闭,或者强制注销,比如
[root@host-192-168-230-107 tecs]# systemctl mask cgred.service
Created symlink from /etc/systemd/system/cgred.service to /dev/null.
# 很明显,就是让启动脚本变成空设备
[root@host-192-168-230-107 tecs]# systemctl status cgred.service
● cgred.service
Loaded: masked (/dev/null; bad)
Active: inactive (dead)
# 记得unmask回来
[root@host-192-168-230-107 tecs]# systemctl unmask cgred.service
Removed symlink /etc/systemd/system/cgred.service.
[root@host-192-168-230-107 tecs]# systemctl status cgred.service
● cgred.service - CGroups Rules Engine Daemon
Loaded: loaded (/usr/lib/systemd/system/cgred.service; disabled; vendor preset: disabled)
Active: inactive (dead)
17.2.2 通过systemctl查看系统上所有的服务
命令格式:systemctl [ command ] [ --type=Type ] [ --all ]
command 主要有:
list-units:默认参数,依据unit显示目前启动的unit,若加上--all才会列出没启动的
list-unit-files:依据/usr/lib/systemd/system/内的文件,将所有文件列表说明
--type=Type:主要有service,socket,target
[tecs@host-192-168-230-107 ~]$ systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File Formats File System Automount Point
……
ceph-osd@11.service loaded active running Ceph object storage daemon osd.11
ceph-osd@15.service loaded active running Ceph object storage daemon osd.15
ceph-osd@19.service loaded active running Ceph object storage daemon osd.19
ceph-osd@2.service loaded active running Ceph object storage daemon osd.2
……
UNIT:名称和类型
LOAD:开机是否会被加载
# 如果只要service 以及 未启动的unit
[tecs@host-192-168-230-107 ~]$ systemctl list-units --type=service
17.2.3 通过systemctl管理操作环境(target unit)
命令格式:systemctl [ command ] [ unit.target ]
command 主要有:
get-default:取得当前的target
set-default:设置新的target为默认的操作模式
isolate :切换到后面接的模式
--type=Type:主要有service,socket,target
和操作界面相关性较高的target主要有:
graphical.target:命令加上图形界面
multi-user.target:纯命令行模式
命令格式:systemctl [ command ] [ unit.target ]
command 主要有:
get-default:获取目前的target
set-default :设置后面接的target为默认的模式
isolate :切换到后面接的模式
[root@host-192-168-230-107 tecs]# systemctl get-default
multi-user.target
[root@host-192-168-230-107 tecs]# 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.
[root@host-192-168-230-107 tecs]# systemctl get-default
graphical.target
[root@host-192-168-230-107 tecs]# systemctl isolate multi-user.target
在target进行正常的切换时,使用isolate的方式即可,不过为了方便,systemd提供了一些简单命令让我们切换操作模式:
systemctl poweroff:系统关机
systemctl reboot:重新开机
systemctl suspend:进入挂起模式
systemctl hibernate:进入休眠模式
systemctl rescue:强制进入恢复模式
systemctl emergency:强制进入紧急恢复模式
挂起是指系统数据保存在内存,关闭系统硬件,没有关机;当用户唤醒系统后,系统数据从内存中恢复,重新驱动硬件,系统正常运行,唤醒速度块
休眠是将系统状态保存在硬盘中,然后关机;当用户唤醒系统后,会将硬盘中的系统状态恢复回来,唤醒速度取决于硬盘速度
17.2.4 通过systemctl分析服务的依赖性
命令格式:systemctl list-dependencies [ unit ] [--reverse]
# 列出当前target环境(mult-user.target)下,用到了那些unit
[root@host-192-168-230-107 tecs]# systemctl list-dependencies
default.target
● ├─acpid.service
● ├─atd.service
● ├─auditd.service
# 列出谁会用到mult-user.target,也就是反向依赖
# 下面可以看出mult-user.target主要是被graphical.target所使用的
[root@host-192-168-230-107 tecs]# systemctl list-dependencies --reverse
default.target
● └─graphical.target
# 如果要查看graphical.target使用的服务
[root@host-192-168-230-107 tecs]# systemctl list-dependencies graphical.target
graphical.target
● ├─diamond.service
● ├─display-manager.service
17.2.5 通过systemctl查看文件目录
[root@host-192-168-230-107 tecs]# systemctl list-
list-dependencies list-jobs list-sockets list-timers list-unit-files list-units
每个目录的优先级和特性见书P575
17.2.6 通过systemctl关闭网络服务
如何查看网络端口
[root@host-192-168-230-107 tecs]# netstat -tlunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 111.111.230.107:6824 0.0.0.0:* LISTEN 34464/ceph-osd
tcp 0 0 100.100.230.107:6824 0.0.0.0:* LISTEN 34451/ceph-osd
tcp 0 0 111.111.230.107:6825 0.0.0.0:* LISTEN 34464/ceph-osd
udp 0 0 0.0.0.0:50783 0.0.0.0:* 9401/avahi-daemon:
udp 0 0 0.0.0.0:5353 0.0.0.0:* 9401/avahi-daemon:
avahi-daemon启用了50783/5353两个端口,看看是否有相关服务
[root@host-192-168-230-107 tecs]# systemctl --all | grep avahi
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
avahi-daemon.socket loaded active running Avahi mDNS/DNS-SD Stack Activation Socket
如果不需要了解即插即用的设备,可以关掉
[root@host-192-168-230-107 tecs]# systemctl stop avahi-daemon.socket avahi-daemon.service
[root@host-192-168-230-107 tecs]# systemctl disable avahi-daemon.socket avahi-daemon.service
Removed symlink /etc/systemd/system/dbus-org.freedesktop.Avahi.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/avahi-daemon.service.
Removed symlink /etc/systemd/system/sockets.target.wants/avahi-daemon.socket.
17.3 systemctl针对service类型的配置文件
17.3.5 自己的服务自己做
# 先制造一个脚本
[root@host-192-168-230-107 backups]# cat backup.sh
source="/test1 /etc"
target="/backups/backup-$(date +%Y-%m-%d-%H-%M).tar.gz"
[ ! -d /backups] && mkdir /backups
tar -zcvf ${target} ${source} &> /backups/1backup.log
#设计一个名为backup.service的启动脚本设置
[root@host-192-168-230-107 backups]# vim /etc/systemd/system/backup.service
[root@host-192-168-230-107 backups]# cat /etc/systemd/system/backup.service
[Unit]
Description=backup test
Requires=atd.service
[Service]
Type=simplei
ExecStart=/bin/bash -c " echo /backups/backup.sh | at now"
[Install]
WantedBy=multi-user.target
# 用start运行
[root@host-192-168-230-107 backups]# systemctl start backup.service
[root@host-192-168-230-107 backups]# ls
1backup.log backup-2022-09-19:1124.tar.gz backup-2022-09-19-11-56.tar.gz backup-2022-09-19-13-49.tar.gz backup-2022-09-19.tar.gz backup.sh