懒猫微服进阶心得(四):每次重启都丢配置?用 systemctl --user 自动装回环境!

在懒猫微服中,为了防止用户误操作破坏系统,默认启用了“重启还原机制”——每次重启都会还原大部分系统改动。不过,用户主目录的数据是保留的(例如 /root/),这就给我们留下了一条生路。

以往每次重启后,我都要手动重新安装 htopsudohttpie 等工具,重复操作实在麻烦。之前在 VIP 群里沟通过能否允许使用 systemctl 自启脚本,现在终于支持了 systemctl --user 的开机启动功能,第一时间来体验一下!


💻 安装脚本 init.sh

我们把需要安装的软件统一写进一个脚本,只安装未安装的部分,避免重复浪费时间。同时也支持远程安装一些工具,例如 superfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash
set -e # 任意步骤失败立即终止

PACKAGES=(
sudo
htop
wget
build-essential
httpie
exa
duf
bat
# 可以继续添加:docker.io nodejs ...
)

need_install=()

for pkg in "${PACKAGES[@]}"; do
if ! dpkg -s "$pkg" &>/dev/null; then
need_install+=("$pkg")
fi
done

if (( ${#need_install[@]} )); then
echo "==> Installing: ${need_install[*]}"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y "${need_install[@]}"
else
echo "==> All packages already installed."
fi

# 安装 Superfile 工具
if command -v curl &>/dev/null; then
echo "==> Installing Superfile (from https://superfile.netlify.app)"
bash -c "$(curl -sLo- https://superfile.netlify.app/install.sh)"
else
echo "❌ curl not found, skipping Superfile install"
fi

你可以把这个脚本保存为 /root/init.sh(懒猫微服会保留这个路径),并赋予执行权限:

1
chmod +x /root/init.sh

⚙ systemd 用户服务配置

由于懒猫微服现在支持 systemctl --user,我们就可以通过用户级 systemd 服务在登录后自动执行该脚本。

~/.config/systemd/user/ 目录下创建服务文件:

1
2
mkdir -p ~/.config/systemd/user
vim ~/.config/systemd/user/bootstrap-packages.service

内容如下:

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Bootstrap Required Packages

[Service]
Type=oneshot # 关键修改!表示一次性任务
ExecStart=/root/init.sh
RemainAfterExit=yes # 任务完成后仍标记为 "active"(可选)
# Restart=no # 默认就是 no,可省略

[Install]
WantedBy=default.target

注意事项:

  • ExecStart 使用 /root/init.sh 是因为懒猫微服重启不会清空 root 目录;
  • 这是一个 oneshot(一次性任务),运行完就退出;
  • default.target 是用户级别的“登录后启动”目标。

🧪 启用和调试服务

配置好之后,使用以下命令启动并设置自动运行:

1
2
3
4
systemctl --user daemon-reload                       # 重新加载用户服务配置
systemctl --user start bootstrap-packages.service # 手动运行一次(测试用)
systemctl --user status bootstrap-packages.service # 查看服务状态和日志
systemctl --user enable bootstrap-packages.service # 设置登录后自动运行

示例运行结果:

1
2
systemctl --user start bootstrap-packages.service
systemctl --user status bootstrap-packages.service

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
○ bootstrap-packages.service - Bootstrap Required Packages
Loaded: loaded (/root/.config/systemd/user/bootstrap-packages.service; enabled; preset: enabled)
Active: inactive (dead) since Thu 2025-05-29 20:13:58 CST; 941ms ago
Duration: 6.438s
Process: 142818 ExecStart=/root/init.sh (code=exited, status=0/SUCCESS)
Main PID: 142818 (code=exited, status=0/SUCCESS)
CPU: 350ms

May 29 20:13:53 lzcbox-029c588e init.sh[142926]: Downloading superfile v1.3.1 for linux (amd64)...
May 29 20:13:58 lzcbox-029c588e init.sh[142926]: Installing superfile...
May 29 20:13:58 lzcbox-029c588e init.sh[142926]: 🎉 Installation complete!
May 29 20:13:58 lzcbox-029c588e init.sh[142926]: You can type "spf" to start!

图示效果如下(安装过程中终端自动拉起):

image-20250529201226160


🚀 小结

步骤 命令
设置 systemd 服务 vim ~/.config/systemd/user/bootstrap-packages.service
测试运行 systemctl --user start bootstrap-packages.service
设置登录自启 systemctl --user enable bootstrap-packages.service
查看运行状态 systemctl --user status bootstrap-packages.service

搭配懒猫微服的 root 持久策略和 systemd 用户服务功能,我们终于实现了:重启自动恢复开发环境,不用每次手动装包了!

77dea8a6a38817c503c379dd946fc9e4.png

懒猫微服进阶心得(四):每次重启都丢配置?用 systemctl --user 自动装回环境!

https://xu-hardy.github.io/懒猫微服进阶心得(四):每次重启都丢配置?用-systemctl-user-自动装回环境!/

作者

Xu

发布于

2025-07-02

更新于

2025-07-01

许可协议

评论