systemctl启动

systemctl启动

1. systemd的配置文件目录

服务启动的配置文件主要放在 /usr/lib/systemd/system 目录,也可能在 /etc/systemd/system 目录

/usr/lib/systemd/system/:每个服务最主要的启动脚本的配置放在这,有点类似以前的/etc/init.d;

/run/systemd/system/:系统执行过程中所产生的服务脚本所在目录,这些脚本的优先级要比/usr/lib/systemd/system/高;

/etc/systemd/system/:管理员根据主机系统的需求所创建的执行脚本所在目录,执行优先级比/run/systemd/system/高;
2. 服务启动分析

(1.) 列出各项启动占用的时间,并行启动,启动时间不决定启动完成先后

systemd-analyze blame

结果:

700ms postgresql-11.service

1.308s zookeeper.service

30ms consul.service

53ms keepalived.service

36ms inte.service

368ms supervisord.service

(2.) 列出启动顺序矢量图

systemd-analyze plot > boot.svg

(3.)查询服务启动依赖

systemd启动顺序约定为,当前服务满足启动条件,则立即创建进程进行并行启动。启动条件指服务的依赖关系(Requires、Wants)及顺序关键字(After、Before)等

systemctl list-dependencies  xxx.service

结果:

systemctl list-dependencies inte.service
inte.service
● ├─postgresql-11.service
● ├─supervisord.service
● ├─system.slice
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-dmesg.service

(4.) 查看所有用timer启动的服务

[root@kfk1 system]# systemctl list-unit-files --type timer
UNIT FILE                    STATE
chrony-dnssrv@.timer         disabled
fstrim.timer                 disabled
iss.timer                  enabled
supervisord.timer            enabled
systemd-readahead-done.timer indirect
systemd-tmpfiles-clean.timer static
3. 服务配置文件

[Unit] 启动顺序与依赖关系

Description:当前服务的简单描述
Documentation:指定 man 文档位置

After:如果 network.target 或 sshd-keygen.service 需要启动,那么 sshd.service 应该在它们之后启动
Before:定义 sshd 应该在哪些服务之前启动
注意:After 和 Before 字段只涉及启动顺序,不涉及依赖关系。

Wants:表示 sshd.service 与 sshd-keygen.service 之间存在"弱依赖"关系,即如果"sshd-keygen.service"启动失败或停止运行,不影响 sshd.service 继续执行
Requires:表示"强依赖"关系,即如果该服务启动失败或异常退出,那么sshd.service 也必须退出
注意:Wants 字段与 Requires 字段只涉及依赖关系,与启动顺序无关,默认情况下是同时启动。

[Service] 启动行为

EnvironmentFile:许多软件都有自己的环境参数文件,该字段指定文件路径
注意:/etc/profile 或者 /etc/profile.d/ 这些文件中配置的环境变量仅对通过 pam 登录的用户生效,而 systemd 是不读这些配置的。
systemd 是所有进程的父进程或祖先进程,它的环境变量会被所有的子进程所继承,如果需要给 systemd 配置默认参数可以在 /etc/systemd/system.conf  和 /etc/systemd/user.conf 中设置。
加载优先级 system.conf 最低,可能会被其他的覆盖。

Type:定义启动类型。可设置:simple,exec,forking,oneshot,dbus,notify,idle
simple(设置了 ExecStart= 但未设置 BusName= 时的默认值):ExecStart 字段启动的进程为该服务的主进程
forking:ExecStart 字段的命令将以 fork() 方式启动,此时父进程将会退出,子进程将成为主进程

ExecStart:定义启动进程时执行的命令
上面的例子中,启动 sshd 执行的命令是 /usr/sbin/sshd -D $OPTIONS,其中的变量 $OPTIONS 就来自 EnvironmentFile 字段指定的环境参数文件。类似的,还有如下字段:
ExecReload:重启服务时执行的命令
ExecStop:停止服务时执行的命令
ExecStartPre:启动服务之前执行的命令
ExecStartPost:启动服务之后执行的命令
ExecStopPost:停止服务之后执行的命令

RemainAfterExit:设为yes,表示进程退出以后,服务仍然保持执行

KillMode:定义 Systemd 如何停止服务,可以设置的值如下:
control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
process:只杀主进程
mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
none:没有进程会被杀掉,只是执行服务的 stop 命令

Restart:定义了退出后,Systemd 的重启方式。可以设置的值如下:
no(默认值):退出后不会重启
on-success:只有正常退出时(退出状态码为0),才会重启
on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
on-abnormal:只有被信号终止和超时,才会重启
on-abort:只有在收到没有捕捉到的信号终止时,才会重启
on-watchdog:超时退出,才会重启
always:不管是什么退出原因,总是重启

RestartSec:表示 Systemd 重启服务之前,需要等待的秒数
[timer] 定时执行

/usr/lib/systemd/system目录里面,新建一个mytimer.timer文件

[Unit]
Description=Runs mytimer every hour

[Timer]
OnUnitActiveSec=1h
Unit=mytimer.service

[Install]
WantedBy=multi-user.target

Timer字段说明:

OnActiveSec:定时器生效后,多少时间开始执行任务
OnBootSec:系统启动后,多少时间开始执行任务
OnStartupSec:Systemd 进程启动后,多少时间开始执行任务
OnUnitActiveSec:该单元上次执行后,等多少时间再次执行
OnUnitInactiveSec: 定时器上次关闭后多少时间,再次执行
OnCalendar:基于绝对时间,而不是相对时间执行
AccuracySec:如果因为各种原因,任务必须推迟执行,推迟的最大秒数,默认是60秒
Unit:真正要执行的任务,默认是同名的带有.service后缀的单元
Persistent:如果设置了该字段,即使定时器到时没有启动,也会自动执行相应的单元
WakeSystem:如果系统休眠,是否自动唤醒系统

定时器相关命令:

systemctl list-timers  # 查看所有正在运行的定时器
sudo systemctl start/stop mytimer.timer  # 启动/停止定时器
systemctl status mytimer.timer # 查看定时器状态
sudo systemctl enable/disable myscript.timer # 开机自启、取消自启

system 日志查看

# 查看整个日志
$ sudo journalctl

# 查看 mytimer.timer 的日志
$ sudo journalctl -u mytimer.timer

# 查看 mytimer.timer 和 mytimer.service 的日志
$ sudo journalctl -u mytimer

# 从结尾开始查看最新日志
$ sudo journalctl -f

# 从结尾开始查看 mytimer.timer 的日志
$ journalctl -f -u timer.timer
4. 常用命令
systemctl #范列出系统上面有启动的unit
 
systemctl list-units --all # 列出所有unit,包括inactive状态的

systemctl list-unit-files #列出所有已经安装的unit有哪些

systemctl list-units --all --state=inactive  # 列出所有未运行的unit,即状态是inactive的
 
systemctl list-units --type=service --all  #列出类型为service的所有项目,不论启动与否
 
systemctl get-default  #输入目前机器默认的模式,如图形界面模式或者文本模式
 
systemctl isolate multi-user.target  #将目前的操作环境改为纯文本模式,关掉图形界面
 
systemctl isolate graphical.target  #将目前的操作环境改为图形界面
 
systemctlpoweroff  #系统关机
 
systemctl reboot   #重新开机
 
systemctl suspend   #进入暂停模式
 
systemctl rescue   #强制进入救援模式
 
systemctl hibernate   #进入休眠模式
 
systemctl emergency   #强制进入紧急救援模式
 
systemctl list-dependencies --reverse   #查询当前默认的target关联了啥
 
systemctl list-dependencies graphical.target  #查询图形界面模式的target关联了啥
 
systemctl list-sockets   #查看当前的socket服务
 
systemctl show etcd.service   #查看 unit 的详细配置情况
 
systemctl mask etcd.service   #禁用某个服务
 
systemctl unmask etcd.service   #解除禁用某个服务

查看服务

# 查询服务状态
systemctl status firewalld

# 添加或修改配置文件后,需要重新加载
systemctl daemon-reload

# 服务是否在运行
systemctl is-active firewalld

# 启动、停止服务
systemctl start/stop firewalld

# 是否开机自启 
systemctl is-enabled firewalld

# 开机自启、禁止
systemctl enable/disable firewalld

# 注销和取消注销服务
 systemctl mask/unmark cups
参考链接

https://blog.csdn.net/xing_huo95/article/details/90246050

https://www.cnblogs.com/jhxxb/p/10654554.html

https://www.cnblogs.com/architectforest/p/12678245.html

http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值