linux systemctl 命令详解

linux systemctl命令详解
systemctl 命令有两大类功能:控制 systemd 系统 和 管理系统上运行的服务
系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其他进程,根据管理,字母d是守护进程(daemon)的缩写,systemd这个名字的含义就是它要守护整个系统。
1检查 systemd 的版本
systemctl --version
2查看 systemd 和 systemctl 程序相关的目录
$ whereis systemd
$ whereis systemctl
管理单个 unit服务
systemctl 提供了一组子命令来管理单个的 unit服务,其命令格式为:
systemctl [command] [unit]
command 主要有:
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 的注销。
我们先通过 nginx.service 来观察服务类型 unit 的基本信息:
[root@localhost ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2020-08-03 04:15:04 EDT; 1h 17min ago
Docs: http://nginx.org/en/docs/
Main PID: 34232 (nginx)
CGroup: /system.slice/nginx.service
├─34232 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─34233 nginx: worker process

Aug 03 04:15:04 localhost.localdomain systemd[1]: Stopped nginx - high performance web server.
Aug 03 04:15:04 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Aug 03 04:15:04 localhost.localdomain systemd[1]: Started nginx - high performance web server.


输出内容的第一行是对 unit 的基本描述。
第二行中的 Loaded 描述操作系统启动时会不会启动这个服务,enabled 表示开机时启动,disabled 表示开机时不启动。而启动该服务的配置文件路径为:(/usr/lib/systemd/system/nginx.service
第三行 中的 Active 描述服务当前的状态,active (running) 表示服务正在运行中。如果是 inactive (dead) 则表示服务当前没有运行。后面则是服务的启动时间。
第四行的 Docs 提供了在线文档的地址。
下面的 Main PID 表示进程的 ID
(接下来展示的有的服务unix有status行,任务的数量,占用的内存和 CPU 资源等,有的则没有,看具体的服务unix类型)
再下面的 Cgroup 描述的是 cgrpup 相关的信息
最后是输出的日志信息。
关于 unit 的启动状态,除了 enable 和 disable 之外还有:
static:这个 unit 不可以自己启动,不过可能会被其它的 enabled 的服务来唤醒。
mask:这个 unit 无论如何都无法被启动!因为已经被强制注销。可通过 systemctl unmask 改回原来的状态。
关于 unit 的运行状态 Active,除了 active 和 inactive 之外还有:
active (exited):仅执行一次就正常结束的服务,目前并没有任何程序在系统中执行。举例来说,开机或者是挂载时才会进行一次的 quotaon 功能,就是这种模式! Quotaon 不需要一直执行,只在执行一次之后,就交给文件系统去自行处理。通常用 bash shell 写的小型服务,大多是属于这种类型。
active (waiting):正在执行当中,不过还再等待其他的事件才能继续处理。举例来说,打印的相关服务就是这种状态。

原文地址:https://www.cnblogs.com/qiangshangkeji/p/13427778.html