# 配置systemd实现rclone开机自动挂载
# 🫎前言
Rclone 是一款管理云存储文件的命令行程序。添加网盘后可以挂载云盘到本机目录,比如将onedrive和Google Drive等知名网盘挂载到本地当作本地目录使用(Linux下如何安装配置Rclone并添加Onedrive网盘及Rclone的基本使用方法),比如在emby中常用在挂载大容量网盘来存储大量的影视文件。但是添加后每次重启都必须重新挂载,非常麻烦,所以配置成为systemd服务文件可以凭借简单的systemctl命令来实现启动停止重启以及开机自启的操作。
# 🦏方法一:直接配置systemd文件
systmd service文件常见的存放位置
/etc/systemd/system/
(优先级最高)/run/systemd/system/
:runtime systemd unit/lib/systemd/system
:安装应用自带的service存储在这里(优先级最低)
通常修改都是在/etc/systemd/system/
所以在/etc/systemd/system/
下创建一个名为rclone-mount.service
的文件。
nano /etc/systemd/system/rclone-mount.service
然后输入以下内容
[Unit]
Description=Mount /opt/movie using rclone
After=network-online.target
[Service]
Type=forking
ExecStart=/usr/bin/rclone mount union:/ /opt/movie --allow-other --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 3h --vfs-cache-max-size 25G --vfs-read-chunk-size-limit 100M --buffer-size 256M --daemon
ExecStop=/bin/fusermount -u /opt/movie
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
这里的ExecStart
部分修改为你自己的rclone挂载参数。ExecStop后的/opt/movie
修改为自己的挂载路径
接下来重新加载systemd,输入:
systemctl daemon-reload
之后就可以使用
systemctl enable rclone-mount.service
来实现开机自启了。
# 🐐方法二:将挂载命令写入脚本,再配置systemd文件
首先创建一个脚本,这里我将脚本放在root目录下
nano /root/rclone_mount.sh
然后在脚本中写入我的挂载命令:
#!/bin/sh
rclone mount union:/ /opt/movie --allow-other --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 3h --vfs-cache-max-size 25G --vfs-read-chunk-size-limit 100M --buffer-size 256M --daemon
在这之后给脚本添加执行权限:
chmod +x /root/rclone_mount.sh
然后配置/etc/systemd/system/rclone-mount.service
nano /etc/systemd/system/rclone-mount.service
填入以下内容:
[Unit]
Description=Mount /opt/movie using rclone
After=network-online.target
[Service]
Type=forking
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/root/rclone_mount.sh
ExecStop=/bin/fusermount -u /opt/movie
Restart=on-failure
[Install]
WantedBy=multi-user.target
接下来重新加载systemd,输入:
systemctl daemon-reload
之后就可以使用
systemctl enable rclone-mount.service
来实现开机自启了。