Systemd服务管理系统

Systemd是现在Linux通用的服务管理系统,已经替代了传统的 System-V init方式。

Systemd

新服务常用的三步:

  • systemctl daemon-reload
  • systemctl enable
  • systemctl start

命令行用法

  • systemctl list-unit-files
  • systemctl start SERVICE.service - Use it to start a service. Does not persist after reboot
  • systemctl stop SERVICE.service - Use it to stop a service. Does not persist after reboot
  • systemctl restart SERVICE.service - Use it to restart a service
  • systemctl reload SERVICE.service - If the service supports it, it will reload the config files related to it without interrupting any process that is using the service.
  • systemctl status SERVICE.service - Shows the status of a serviceTells whether a service is currently running.
  • systemctl enable SERVICE.service - Turns the service on, on the next reboot or on the next start event. It persists after reboot.
  • systemctl disable SERVICE.service - Turns the service off on the next reboot or on the next stop event. It persists after reboot.
  • systemctl is-enabled SERVICE.service - Check if a service is currently configured to start or not on the next reboot.
  • systemctl is-active SERVICE.service - Check if a service is currently active.
  • systemctl show SERVICE.service - Show all the information about the service.

Journald日志系统

systemd里用于收集和存储日志的组件(systemd-journald)。 用于捕获系统日志信息、内核日志信息,以及来自原始RAM磁盘的信息,早期启动信息以及所有服务中写入STDOUT和STDERR数据流的信息。

具有以下特点:

  • 存储结构化数据
  • 允许多字段,多行

但缺乏很好的远程中心化日志支持。

journalctl

属于服务systemd-journald, 日志存放目录在/var/log/journal

# 查看最新日志
journalctl -r

# 查看磁盘占用
du -sh /var/log/journal/
journalctl --disk-usage

# 只保留最近2天的日志
journalctl --vacuum-time=2d

日志详情

journald日志写入二进制文件。

在journald上,没有必要循环日志文件。它构建的目的在于监控存储卷上的剩余空间。如果卷快满了,就删除旧有记录释放空间。

要为journald日志设置一个最大尺寸,在/etc/systemd/journal.conf文件中修改SystemMaxUse参数。

如果配置里 Storage=auto(默认), 则存放在 /run/log/journal//*.journal[~]

设置为Storage=persistent 以存在磁盘.

journalctl日志查看

  • journalctl -b : 自启动后日志
  • journalctl –list-boots

时间区间过滤:

  • journalctl –since “2021-01-10” –until “2021-01-11 03:00”
  • journalctl –since yesterday
  • journalctl –since 09:00 –until “1 hour ago”

其他过滤项:

  • journalctl -u nginx.service 基于服务过滤
  • journalctl _PID=1234
  • journalctl -p err 基于日志级别

内核信息:journalctl -k (等价于dmesg)

历史遗留init系统

脚本通常放在/etc/init.d, 使用以下命令创建软连接并放在诸如/etc/rc2.d的文件夹。

K代表停止, S代表启动.

chkconfig (CentOS)

  • chkconfig –list
  • chkconfig on : 系统重启时启动
  • chkconfig off
  • chkconfig –add

run levels:

  • 3 多用户,command only
  • 5 多用户,X-Windows
  • 6 reboot

update-rc.d (Ubuntu)

update-rc.d - install and remove System-V style init script links

  • update-rc.d –help
  • update-rc.d redis-server defaults

注: Ubutnu引入的Upstart已处于维护阶段(配置通常位于/etc/init/*.conf)

扩展阅读