systemd

创建服务

运行目录

1
2
sudo mkdir /opt/nfs-mount-239.service
cd /opt/nfs-mount-239.service

启动 Shell

1
sudo vim start.sh
1
2
3
#!/bin/bash
# mount nfs
mount -t nfs 10.0.0.239:/opt/gitlab-runner/android-sdk /mnt/android-sdk-remote-nfs -o nolock
1
sudo chmod +x start.sh

停止 Shell

1
sudo vim stop.sh
1
2
3
#!/bin/bash
# umount nfs
umount /mnt/android-sdk-remote-nfs
1
sudo chmod +x stop.sh

配置文件

1
sudo vim /lib/systemd/system/nfs-mount-239.service

Unit 配置参考文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=mount nfs from 10.0.0.239
Wants=network-online.target
BindsTo=nfs-client.target
After=ssh.service

[Service]
WorkingDirectory=/opt/nfs-mount-239.service
ExecStart=/opt/nfs-mount-239.service/start.sh
Restart=on-failure
ExecReload=/opt/nfs-mount-239.service/stop.sh
RestartSec=10

[Install]
WantedBy=multi-user.target
Alias=nfs-mount-239

配置

配置文件修改后,让 SystemD 重新加载配置文件

1
sudo systemctl daemon-reload

检查服务状态

1
sudo systemctl status nfs-mount-239

启动服务

1
sudo systemctl start nfs-mount-239

停止服务

1
sudo systemctl stop nfs-mount-239

设置开机启动

1
sudo systemctl enable nfs-mount-239

禁止开机启动

1
sudo  systemtl disable  nfs-mount-239

检查开机启动配置结果

1
sudo systemctl is-enabled nfs-mount-239

enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。

调试 & 分析

日志

启动顺序

1
systemd-analyze plot > boot.svg

启动耗时

1
systemd-analyze blame