Linux 服务管理两种方式service和systemctl
Linux 服务管理两种方式service和systemctl
1.service命令
service命令其实是去/etc/init.d目录下,去执行相关程序
# service命令启动redis脚本
service redis start
# 直接启动redis脚本
/etc/init.d/redis start
# 开机自启动
update-rc.d redis defaults
其中脚本需要我们自己编写
2.systemctl命令
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl
1)systemctl命令兼容了service
即systemctl也会去/etc/init.d目录下,查看,执行相关程序
systemctl redis start
systemctl redis stop
# 开机自启动
systemctl enable redis
2)systemctl命令管理systemd的资源Unit
systemd的Unit放在目录/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu)
主要有四种类型文件.mount,.service,.target,.wants
- .mount文件
.mount文件定义了一个挂载点,[Mount]节点里配置了What,Where,Type三个数据项
等同于以下命令:
mount -t hugetlbfs /dev/hugepages hugetlbfs
- .service文件
.service文件定义了一个服务,分为[Unit],[Service],[Install]三个小节
[Unit]
Description:描述,
After:在network.target,auditd.service启动后才启动
ConditionPathExists: 执行条件
[Service]
EnvironmentFile:变量所在文件
ExecStart: 执行启动脚本
Restart: fail时重启
[Install]
Alias:服务别名
WangtedBy: 多用户模式下需要的
- .target文件
.target定义了一些基础的组件,供.service文件调用
- .wants文件
.wants文件定义了要执行的文件集合,每次执行,.wants文件夹里面的文件都会执行
systemctl 命令 unit
Unit(单元|服务)
Systemd 可以管理所有系统资源:
- 将系统资源划分为12类
- 将每个系统资源称为一个 Unit。Unit 是 Systemd 管理系统资源的基本单位
- 使用一个 Unit File 作为 Unit 的单元文件,Systemd 通过单元文件控制 Unit 的启动
例如,MySQL服务被 Systemd 视为一个 Unit,使用一个 mysql.service 作为启动配置文件
Unit File(单元文件|配置文件)
单元文件中包含该单元的描述、属性、启动命令等
类型
Systemd 将系统资源划分为12类,对应12种类型的单元文件
系统资源类型 | 单元文件扩展名 | 单元文件描述 |
---|---|---|
Service | .service | 封装守护进程的启动、停止、重启和重载操作,是最常见的一种 Unit 文件 |
Target | .target | 定义 target 信息及依赖关系,一般仅包含 Unit 段 |
Device | .device | 对于 /dev 目录下的硬件设备,主要用于定义设备之间的依赖关系 |
Mount | .mount | 定义文件系统的挂载点,可以替代过去的 /etc/fstab 配置文件 |
Automount | .automount | 用于控制自动挂载文件系统,相当于 SysV-init 的 autofs 服务 |
Path | .path | 用于监控指定目录或文件的变化,并触发其它 Unit 运行 |
Scope | .scope | 这种 Unit 文件不是用户创建的,而是 Systemd 运行时产生的,描述一些系统服务的分组信息 |
Slice | .slice | 用于表示一个 CGroup 的树 |
Snapshot | .snapshot | 用于表示一个由 systemctl snapshot 命令创建的 Systemd Units 运行状态快照,可以切回某个快照 |
Socket | .socket | 监控来自于系统或网络的数据消息 |
Swap | .swap | 定义一个用户做虚拟内存的交换分区 |
Timer | .timer | 用于配置在特定时间触发的任务,替代了 Crontab 的功能 |
对于操作单元文件的命令,如果缺省扩展名,则默认.service扩展名,而操作 target 的命令,例如 isolate,则默认.target扩展名
systemctl 参数说明
1、使用语法
用法:systemctl [OPTIONS…] {COMMAND} …
2 、参数说明
参数 | 参数说明 |
---|---|
start | 立刻启动后面接的unit |
stop | 立刻关闭后面接的unit |
restart | 立刻关闭后启动后面接的unit,亦即执行stop再start的意思 |
reload | 不关闭后面接的unit的情况下,重载配置文件,让设定生效 |
enable | 设定下次开机时,后面接的unit会被启动 |
disable | 设定下次开机时,后面接的unit 不会被启动 |
status | 目前后面接的这个unit 的状态,会列出是否正在执行、是否开机启动等信息。 |
is-active | 目前有没有正在运行中 |
is-enable | 开机时有没有预设要启用这个unit |
kill | 不要被kill这个名字吓着了,它其实是向运行unit的进程发送信号 |
show | 列出unit的配置。 |
mask | 注销unit,注销后你就无法启动这个unit了 |
unmask | 取消对unit的注销 |
list-units | 依据unit列出目前有启动的unit。若加上–all才会列出没启动的。(等价于无参数) |
list-unit-files | 列出所有以安装unit以及他们的开机启动状态(enabled、disabled、static、mask)。 |
–type=TYPE 就是unit type,主要有service,socket,target等 | |
get-default | 取得目前的 target |
set-default | 设定后面接的 target 成为默认的操作模式 |
isolate | 切换到后面接的模式 |
systemctl 描述
任务 | 旧指令 | 新指令 |
---|---|---|
使某服务自动启动 | chkconfig --level 3 | httpd on systemctl enable httpd.service |
使某服务不自动启动 | chkconfig --level 3 httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status systemctl status httpd.service (服务详细信息) | systemctl is-enabled httpd.service (仅显示是否 Active) |
显示所有已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
停止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
某服务重新加载配置文件 | service httpd reload | systemctl reload httpd.service |
systemctl 示例
列出所有可用单元
systemctl list-unit-files |more
systemctl list-units --type=target
列出所有已加载单元
systemctl list-units |more
查看可用systemctl管理的所有服务
systemctl list-units --type=service
systemctl可用管理单元分很多种,日常工作中我们仅仅用于管理服务,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, 用于定义文件系统中的一文件或目录;
注销服务
服务被注销后该服务就无法通过systemctl进行启停管理。
[root@s153 system]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@s153 system]# systemctl start firewalld
Failed to start firewalld.service: Unit is masked.
取消注销服务
[root@s153 system]# systemctl unmask firewalld
Removed symlink /etc/systemd/system/firewalld.service.
[root@s153 system]# systemctl start firewalld
设置服务开机自启动
[root@s153 system]# systemctl enable xinetd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/xinetd.service to /usr/lib/systemd/system/xinetd.service.
取消服务开机自启动
[root@s153 system]# systemctl disable xinetd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.
查看机器信息
[root@s153 system]# systemctl list-machines
NAME STATE FAILED JOBS
s153 (host) running 0 0
1 machines listed.
查看系统环境变量
[root@s153 system]# systemctl show-environment
LANG=zh_CN.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
重新加载unit文件
如果手动修改了unit文件,可以使用此命令重新加载。
[root@s153 system]# systemctl daemon-reload
创建一个系统快照
[root@s153 system]# systemctl snapshot wuhs
wuhs.snapshot
删除指定快照
[root@s153 system]# systemctl delete wuhs
查看服务是否开机自启动
[root@s153 system]# systemctl is-enabled xinetd.service
enabled
杀死服务
[root@s153 system]# systemctl kill xinetd
[root@s153 system]# systemctl is-failed xinetd
inactive
进入救援模式
[root@s153 system]# systemctl rescue
Broadcast message from root@s153 on pts/1 (三 2022-07-20 13:08:30 CST):
The system is going down to rescue mode NOW!
#救援模式下切换到默认模式
[root@s153 ~]# systemctl default
关闭系统
[root@s153 ~]# systemctl poweroff
重启机器
[root@s153 ~]# systemctl reboot
系统睡眠
suspend暂停模式,类似window环境的睡眠模式,会将系统的状态数据保存到内存中,然后关闭掉大部分的系统硬件,当然,并没有实际关机。当用户按下唤醒机器的按钮,系统数据会重内存中回复,然后重新驱动被大部分关闭的硬件,就开始正常运作!唤醒的速度较快。
[root@s153 ~]# systemctl suspend
查看系统启动模式
[root@s153 boot]# systemctl get-default
multi-user.target
设置系统为图形界面启动
[root@s153 system]# 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.