美文网首页
Linux下systemctl基本用法

Linux下systemctl基本用法

作者: CodingCode | 来源:发表于2020-04-18 03:17 被阅读0次

Linux下systemctl基本用法

  1. systemctl和systemd的关系

systemd是系统管理的守护进程,用来取代system v的初始进程;我们可以看到systemd的进程号就是1。

$ ps -ef | grep systemd
root         1     0  0 Apr15 ?        00:04:29 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root      1390     1  0 Apr15 ?        00:00:02 /usr/lib/systemd/systemd-udevd
dbus      1832     1  0 Apr15 ?        00:00:41 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root      1838     1  0 Apr15 ?        00:00:14 /usr/lib/systemd/systemd-logind
root      6579     1  0 17:26 ?        00:00:04 /usr/lib/systemd/systemd-journald

而且/usr/sbin/init就是指向systemd的链接:

$ ls -l /usr/sbin/init 
lrwxrwxrwx 1 root root 22 Mar 16 18:27 /usr/sbin/init -> ../lib/systemd/systemd

然后sytemctl是什么呢?
systemctl就是一个负责和守护进程systemd通信的客户端工具。

  1. 查看系统unit

列出所有unit

$ systemctl
  UNIT                           LOAD   ACTIVE     SUB          DESCRIPTION
  sys-module-configfs.device     loaded active     plugged      /sys/module/configfs
  boot.mount                     loaded active     mounted      /boot
  var.mount                      loaded active     mounted      /var
  auditd.service                 loaded active     running      Security Auditing Service
  kdump.service                  loaded active     exited       Crash recovery kernel arming
  systemd-journald.service       loaded active     running      Journal Service
  systemd-logind.service         loaded active     running      Login Service
  docker.socket                  loaded active     running      Docker Socket for the API
...
$ systemctl --type=service  # list all service

类型type就是unit名字里面'.'之后的部分。

  1. 基本服务操作
$ systemctl start   <docker>.service
$ systemctl restart <docker>.service
$ systemctl stop    <docker>.service
$ systemctl reload  <docker>.service
$ systemctl enable  <docker>.service
$ systemctl disable <docker>.service
  1. 检查服务状态
$ systemctl status     <docker>.service
$ systemctl is-active  <docker>.service    # whether service is running
$ systemctl is-enabled <docker>.service    # whether service is auto started at system boot
  1. 检查服务配置
$ systemctl show <docker>.service
  1. 还有电脑关机和重启
$ sudo systemctl poweroff
$ sudo systemctl reboot
  1. daemon-reload

要对systemctl daemon-reload单独做说明。

我们可以把damon理解为systemd,于是命令就变成systemctl systemd-reload,这样就比较好理解了:重新加载 systemd的配置文件。而所有子服务的配置文件都算是systemd的配置文件,因为systemd管理所有的子服务;所以任何服务的配置发生改变都应该执行deamon-reload命令,包括:

  1. 添加新的服务
  2. 添加新的服务配置文件
  3. 修改服务配置文件

systemd维护着所有子服务的列表,当任何一个配置发生改变时,都应该通知systemd。

  1. 创建一个服务

创建服务定义文件

$ /etc/systemd/system/<servciename>.service
[Unit]
Description=<Service Name>
After=network-online.target docker.service

[Service]
Type=simple
Restart=always
ExecStartPre=-/usr/bin/docker rm -f <servicecontainer>
ExecStart=/usr/bin/docker run --name <servicecontainer> <serviceimage>
ExecStop=/usr/bin/docker stop -t 2 <servicecontainer>
TimeoutStartSec=20
TimeoutStopSec=20
User=<username>
Group=<groupname>

[Install]
WantedBy=default.target

安装到systemd,并且配置成系统自启动:

$ sudo systemctl daemon-reload
$ sudo systemctl enable <servicename>.service

相关文章

网友评论

      本文标题:Linux下systemctl基本用法

      本文链接:https://www.haomeiwen.com/subject/jjzpvhtx.html