systemd简介

简介: systemd是Linux的一个工具,是CentOS7.x系统启动的第一个进程1.systemd的一些相关命令(1)systemctl 系统管理#检查系统是否充分的运行[root@localhost ~]# systemctl is-system-runningr...

systemd是Linux的一个工具,是CentOS7.x系统启动的第一个进程


1.systemd的一些相关命令

(1)systemctl 系统管理

#检查系统是否充分的运行

[root@localhost ~]# systemctl is-system-running

running


#进入系统默认模式

[root@localhost ~]# systemctl default

PolicyKit daemon disconnected from the bus.

We are no longer a registered authentication agent.


#进入营救模式

[root@localhost ~]# systemctl rescue

Broadcast message from root@localhost.localdomain on pts/0 (Mon 2018-03-05 23:25:23 CST):

The system is going down to rescue mode NOW!

[root@localhost ~]# systemctl is-system-running

maintenance


#进入紧急情况模式

[root@localhost ~]# systemctl emergency


#进入冬眠模式

[root@localhost ~]# systemctl hibernate 


[root@localhost ~]# systemctl half

[root@localhost ~]# systemctl poweroff

[root@localhost ~]# systemctl reboot


(2)systemd-analyze查看启动耗时

[root@localhost ~]# systemd-analyze

Startup finished in 849ms (kernel) + 2.347s (initrd) + 20.205s (userspace) = 23.402s


#查看每个服务启动耗时

[root@localhost ~]# systemd-analyze blame


#查看指定服务的启动流

[root@localhost ~]# systemd-analyze critical-chain httpd.service

The time after the unit is active or started is printed after the "@" character.

The time the unit takes to start is printed after the "+" character.

httpd.service +115ms

└─system.slice

  └─-.slice


[root@localhost ~]# systemd-analyze critical-chain sshd.service

The time after the unit is active or started is printed after the "@" character.

The time the unit takes to start is printed after the "+" character.

sshd.service @9min 39.109s

└─system.slice

  └─-.slice


(3)hostnamectl

#查看主机名等信息

[root@localhost ~]# hostnamectl


#修改主机名,exit会话或者reboot生效

[root@localhost ~]# hostnamectl set-hostname


(4)localectl

#查看本地化设置

[root@darren ~]# localectl

   System Locale:.UTF-8

       VC Keymap: us

      X11 Layout: us


[root@darren ~]# localectl set-locale.UTF-8

[root@darren ~]# localectl list-locales|grep 'zh_CN'


(5)timedatectl

[root@darren ~]# timedatectl

      Local time: Tue 2018-03-06 19:05:28 CST

  Universal time: Tue 2018-03-06 11:05:28 UTC

        RTC time: Tue 2018-03-06 12:22:53

       Time zone: Asia/Shanghai (CST, +0800)

     NTP enabled: n/a

NTP synchronized: no

RTC in local TZ: no

      DST active: n/a


[root@darren ~]# timedatectl list-timezones


#设置时间

[root@darren ~]# timedatectl set-time 2018-03-06

[root@darren ~]# timedatectl set-time 20:31


(6)loginctl用户连接相关信息

Tue Mar  6 20:31:53 CST 2018

[root@darren ~]# loginctl

   SESSION        UID USER             SEAT            

        19          0 root                             

        24          0 root                             

        23          0 root


[root@darren ~]# loginctl list-users

       UID USER            

         0 root


#查看root用户的详细信息

[root@darren ~]# loginctl show-user root




2.服务管理systemctl

(1)自启动原理

对于支持systemd的软件,安装后,会自动在/usr/lib/systemd/system目录下生成一个xxx.service配置文件.

系统启动的时候会读取/etc/systemd/system下的配置文件,所以会开启自启动。


例:

开启httpd自启动的实质是在/etc/systemd/system/multi-user.target.wants下创建一个软连接指向/usr/lib/systemd/system/httpd.service文件

[root@localhost system]# systemctl is-enabled httpd.service

disabled


[root@localhost system]# systemctl enable  httpd.service

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.


[root@localhost ~]# ll /etc/systemd/system/multi-user.target.wants/httpd.service

lrwxrwxrwx. 1 root root 37 Feb 24 04:03 /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service



启动一个服务:systemctl start postfix.service

关闭一个服务:systemctl stop postfix.service

重启一个服务:systemctl restart postfix.service

显示一个服务的状态:systemctl status postfix.service

开启开机自启动:systemctl enable postfix.service

关闭开机自启动:systemctl disable postfix.service

查看服务是否开机启动:systemctl is-enabled postfix.service

查看服务是否活动:systemctl is-active httpd.service

查看已启动的服务列表:systemctl list-unit-files|grep enabled

删除httpd进程: systemctl kill httpd.service

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

查看服务的依赖关系:systemctl list-dependencies httpd.service




(2)列出所有unit及配置文件的状态

配置文件状态种类:

enabled:已建立自启动链接

disabled:没有建立自启动链接

static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖

masked:该配置文件被禁止建立自启动链接


[root@darren system]# systemctl list-unit-files

autovt@.service                             disabled

brandbot.service                            static  

httpd.service                               enabled


(3)配置文件的格式说明

[root@darren system]# cat httpd.service

[Unit]

Description=The Apache HTTP Server

After=network.target remote-fs.target nss-lookup.target

Documentation=man:httpd(8)

Documentation=man:apachectl(8)

[Service]

Type=notify

EnvironmentFile=/etc/sysconfig/httpd

#ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND

ExecReload=/usr/sbin/httpd $OPTIONS -k graceful

ExecStop=/bin/kill -WINCH ${MAINPID}

# We want systemd to give httpd some time to finish gracefully, but still want

# it to kill httpd after TimeoutStopSec if something went wrong during the

# graceful stop. Normally, Systemd sends SIGTERM signal right after the

# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give

# httpd time to finish.

KillSignal=SIGCONT

PrivateTmp=true

[Install]

WantedBy=multi-user.target


[Unit]区块通常是配置文件的第一个区块,用来定义Unit的元数据,以及该服务与其他Unit的关系

Description:描述信息

Documentation:文档地址

After:在httpd之前需要启动的unit

Before:在httpd之后需要启动的unit

Requires:依赖的unit启动,该unit才能启动,如After模块的unit没有运行,当前Unit则不会启动

Wants:和Requires相似,但是没有它严格,如After模块的unit没有运行,当前Unit也可成功启动个(推荐使用)


[Service]服务的配置

Type选项:

Type=simple :(默认值) systemd认为该服务将立即启动。服务进程不会 fork 。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket 激活型。

Type=forking :systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便 systemd 能够跟踪服务的主进程。

Type=oneshot :这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

Type=notify :与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

Type=dbus :若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

Type=idle :systemd会等待所有任务处理完成后,才开始执行 idle 类型的单元。其他行为与 Type=simple 类似。



[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。

WantedBy=multi-user.target



3.日志管理journalctl

配置文件:/etc/systemd/journald.conf


journalctl -b -0 显示本次启动的信息

journalctl -b -1 显示上次启动的信息

journalctl -b -2 显示上上次启动的信息 journalctl -b -2


只显示错误、冲突和重要告警信息

journalctl -p err..alert   等价于: journalctl -p 3..1


显示从某个日期 ( 或时间 ) 开始的消息:

 journalctl --since="2012-10-30 18:17:16"


显示从某个时间 ( 例如 20分钟前 ) 的消息:

journalctl --since "20 min ago"


显示最新信息

 journalctl -f


显示特定程序的所有消息:

# journalctl /usr/lib/systemd/systemd


显示特定进程的所有消息:

journalctl _PID=1


参考链接:https://wiki.archlinux.org/index.php/systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E5.A4.84.E7.90.86.E4.BE.9D.E8.B5.96.E5.85.B3.E7.B3.BB



目录
相关文章
|
8月前
|
Linux 开发工具
18.4 【Linux】systemd-journald.service 简介
18.4 【Linux】systemd-journald.service 简介
77 0
|
7月前
|
存储 Cloud Native Java
Linux下systemd深入指南:如何优化Java服务管理与开机自启配置
Linux下systemd深入指南:如何优化Java服务管理与开机自启配置
157 0
|
12月前
|
监控 Java 数据库连接
服务--systemd的管理
服务--systemd的管理
122 0
|
Linux
【Linux】systemd 配置文件
【Linux】systemd 配置文件
202 0
|
Linux 数据中心 Python
如何使用systemd部署服务?
如何使用systemd部署服务?
413 0
|
运维 小程序 Linux
systemd攻略之三:如何利用systemd控制cgroup,实战
经过最近研究,发现对systemd如何利用cgroup的实例少之又少,而且,很多人搞不清,在el7上,如果想使用cgroup到底怎么使用?到底该如何systemd为一个进程或者服务利用cgroup?
3955 1
systemd攻略之三:如何利用systemd控制cgroup,实战
|
运维 网络协议 关系型数据库
Systemd | 学习笔记
快速学习Systemd,掌握如何进行服务的管理和配置,并引导学生主动完成服务的编写,为后续的运维工作打下基础
Systemd | 学习笔记
|
监控 关系型数据库 网络安全
|
监控 应用服务中间件 Linux