Systemd
Systemd
$ sudo /etc/init.d/apache2 start
# 或者
$ service apache2 start
当
# 查看 Systemd 的版本
$ systemctl --version
# 重启系统
$ sudo systemctl reboot
# 关闭系统,切断电源
$ sudo systemctl poweroff
# CPU 停止工作
$ sudo systemctl halt
# 查看启动耗时
$ systemd-analyze
# 查看每个服务的启动耗时
$ systemd-analyze blame
# 显示瀑布状的启动过程流
$ systemd-analyze critical-chain
# 显示指定服务的启动流
$ systemd-analyze critical-chain atd.service
- Service unit:系统服务
- Target unit:多个
Unit 构成的一个组 - Device Unit:硬件设备
- Mount Unit:文件系统的挂载点
- Automount Unit:自动挂载点
- Path Unit:文件或路径
- Scope Unit:不是由
Systemd 启动的外部进程 - Slice Unit:进程组
- Snapshot Unit:
Systemd 快照,可以切回某个快照 - Socket Unit:进程间通信的
socket - Swap Unit:
swap 文件 - Timer Unit:定时器
# 显示系统状态
$ systemctl status
# 显示单个 Unit 的状态
$ sysystemctl status bluetooth.service
# 显示远程主机的某个 Unit 的状态
$ systemctl -H root@rhel7.example.com status httpd.service
我们最常用的就是
# 立即启动一个服务
$ sudo systemctl start apache.service
# 立即停止一个服务
$ sudo systemctl stop apache.service
# 重启一个服务
$ sudo systemctl restart apache.service
# 杀死一个服务的所有子进程
$ sudo systemctl kill apache.service
# 重新加载一个服务的配置文件
$ sudo systemctl reload apache.service
# 重载所有修改过的配置文件
$ sudo systemctl daemon-reload
# 显示某个 Unit 的所有底层参数
$ systemctl show httpd.service
# 显示某个 Unit 的指定属性的值
$ systemctl show -p CPUShares httpd.service
# 设置某个 Unit 的指定属性
$ sudo systemctl set-property httpd.service CPUShares=500
每一个/etc/systemd/system/
读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/
,真正的配置文件存放在那个目录。
[Unit]
Description=ATD daemon
[Service]
Type=forking
ExecStart=/usr/bin/atd
[Install]
WantedBy=multi-user.target
1 | systemcel enable nginx.service |
---|---|
设置开机启动即可。
设置开机启动即可。在这里我是用源码编译安装的,所以要手动创建
设置开机启动即可。在这里我是用源码编译安装的,所以要手动创建
1 | /lib/systemd/system/ |
---|---|
1 | vim /lib/systemd/system/nginx.service |
---|---|
内容如下
12345678910111213 | [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true [Install] WantedBy=multi-user.target |
---|---|
[]: Unit “服务的说明” []: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
[]: Unit “服务的说明”
保存退出。
1 | systemctl enable nginx.service |
---|---|
1 | systemctl start nginx.service |
---|---|
设置开机自启动
1 | systemctl enable nginx.service |
---|---|
停止开机自启动
1 | systemctl disable nginx.service |
---|---|
查看服务当前状态
1 | systemctl status nginx.service |
---|---|
重新启动服务
1 | systemctl restart nginx.service |
---|---|
查看所有已启动的服务
1 | systemctl list-units –type=service |
---|---|
备注 | ||
---|---|---|
service foo start | systemctl start foo.service | 用来启动一个服务 |
service foo stop | systemctl stop foo.service | 用来停止一个服务 |
service foo restart | systemctl restart foo.service | 用来停止并启动一个服务, |
service foo reload | systemctl reload foo.service | 当支持时,重新装载配置文件而不中断等待操作, |
service foo condrestart | systemctl condrestart foo.service | 如果服务正在运行那么重启它, |
service foo status | systemctl status foo.service | 汇报服务是否正在运行, |
ls /etc/rc.d/init.d/ | systemctl list-unit-files –type=service | 用来列出可以启动或停止的服务列表, |
chkconfig foo on | systemctl enable foo.service | 在下次启动时或满足其他触发条件时设置服务为启用 |
chkconfig foo off | systemctl disable foo.service | 在下次启动时或满足其他触发条件时设置服务为禁用 |
chkconfig foo | systemctl is-enabled foo.service | 用来检查一个服务在当前环境下被配置为启用还是禁用 |
chkconfig –list | systemctl list-unit-files –type=service | 输出在各个运行级别下服务的启用和禁用情况 |
chkconfig foo –list | ls /etc/systemd/system/*.wants/foo.service | 用来列出该服务在哪些运行级别下启用和禁用, |
chkconfig foo –add | systemctl daemon-reload | 当您创建新服务文件或者变更设置时使用, |
telinit 3 | systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) | 改变至多用户运行级别, |
备注 | ||
---|---|---|
0 | runlevel0.target, poweroff.target | 关闭系统, |
1, s, single | runlevel1.target, rescue.target | 单用户模式, |
2, 4 | runlevel2.target, runlevel4.target, multi-user.target | 用户定义 |
3 | runlevel3.target, multi-user.target | 多用户,非图形化。用户可以通过多个控制台或网络登录, |
5 | runlevel5.target, graphical.target | 多用户,图形化。通常为所有运行级别 |
6 | runlevel6.target, reboot.target | 重启 |
emergency | emergency.target | 紧急 |