Skip to content

懒猫微服开机初始化脚本 ​

微服重启会重置系统修改的内容,想要自己安装一些软件或者开机执行特定的脚本,就可以使用下面的方法。

创建开机脚本 ​

root 的家目录是不会还原的,我们的配置都在是家目录里面。

我们要创建 systemd 的启动脚本,和需要执行的脚本文件,

shell
# 创建 systemd 的用户目录
mkdir -p ~/.config/systemd/user

# 写入开机脚本

cat  ~/.config/systemd/user/bootstrap-packages.service < EOF
[Unit]
Description=Bootstrap Required Packages

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

[Install]
WantedBy=default.target
EOF

# 创建开机启动脚本, 我这里是安装一些常用工具
cat ~/init.sh < EOF
#!/bin/bash

apt update

apt install -y fish \
  neovim \
  btop \
  htop \
  gdu \
  tmux \
  kitty-terminfo \
  git
EOF

chmod +x ~/init.sh

设置服务开机启动

shell
systemctl enable --user auto-start-install.service

检查服务状态

shell
systemctl status --user auto-start-install.service