linux使用服务添加一个开机启动脚本

以下是创建 user-plan.sh 开机自启动服务的完整步骤,包含服务配置、权限设置和调试方法:


1. 准备脚本文件

user-plan.sh 放到合适位置(如 /opt/user-plan/),并赋予可执行权限:

bash 复制代码
sudo mkdir -p /opt/user-plan/
sudo cp /path/to/user-plan.sh /opt/user-plan/
sudo chmod +x /opt/user-plan/user-plan.sh

2. 创建 systemd 服务文件

新建服务配置文件 /etc/systemd/system/user-plan.service,内容如下:

ini 复制代码
[Unit]
Description=User Plan Service
After=network.target  # 若需依赖网络,否则可删除此行

[Service]
Type=simple
ExecStart=/opt/user-plan/user-plan.sh  # 必须绝对路径
Restart=on-failure    # 失败时自动重启
User=root             # 根据需要替换用户(如 www-data)
WorkingDirectory=/opt/user-plan/  # 脚本的工作目录
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # 确保环境变量

# 可选资源限制
MemoryLimit=500M      # 限制内存使用
CPUShares=512

[Install]
WantedBy=multi-user.target  # 多用户模式启动时生效

3. 启用服务

bash 复制代码
# 重载 systemd 配置
sudo systemctl daemon-reload

# 启动服务并设置开机自启
sudo systemctl start user-plan
sudo systemctl enable user-plan

4. 验证服务状态

bash 复制代码
# 查看服务是否运行
sudo systemctl status user-plan

# 实时查看服务日志(调试必用)
sudo journalctl -u user-plan.service -f

5. 常见问题处理

问题1:权限不足
  • 现象 :日志报错 Permission denied

  • 解决

    bash 复制代码
    sudo chmod 755 /opt/user-plan/user-plan.sh
    sudo chown root:root /opt/user-plan/user-plan.sh  # 根据实际用户调整
问题2:脚本路径错误
  • 现象 :日志报错 No such file or directory
  • 解决
    • 检查 ExecStart 路径是否绝对路径
    • 执行 ls -l /opt/user-plan/user-plan.sh 确认文件存在
问题3:环境变量缺失
  • 现象 :脚本中命令无法识别(如 python3 未找到)
  • 解决
    • [Service] 中明确指定环境变量:

      ini 复制代码
      Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

6. 扩展配置(按需选择)

配置脚本参数

若脚本需要传参:

ini 复制代码
ExecStart=/opt/user-plan/user-plan.sh --mode=production
开机延迟启动

避免系统启动时资源竞争:

ini 复制代码
[Service]
ExecStartPre=/bin/sleep 10  # 延迟10秒启动
自定义日志输出

将脚本日志写入文件:

ini 复制代码
StandardOutput=file:/var/log/user-plan.log
StandardError=file:/var/log/user-plan-error.log

完整操作示例

bash 复制代码
# 从创建文件到启用服务的全流程
echo '#!/bin/bash
while true; do
  echo "User plan service is running $(date)" >> /var/log/user-plan.log
  sleep 60
done' | sudo tee /opt/user-plan/user-plan.sh

sudo chmod +x /opt/user-plan/user-plan.sh

sudo tee /etc/systemd/system/user-plan.service > /dev/null <<'EOF'
[Unit]
Description=User Plan Demo Service

[Service]
Type=simple
ExecStart=/opt/user-plan/user-plan.sh
Restart=always

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now user-plan

通过以上步骤,你的 user-plan.sh 脚本即可作为系统服务开机自动运行。如需进一步优化,可根据实际业务需求调整服务配置参数。

相关推荐
惊起白鸽4501 小时前
LVS+Keepalived高可用群集
linux·服务器·lvs
潘yi.1 小时前
LVS+Keepalived高可用群集
linux·服务器·lvs·keepalived
洁✘1 小时前
LVS-DR群集
linux·服务器·lvs
jz_ddk1 小时前
[学习] C语言多维指针探讨(代码示例)
linux·c语言·开发语言·学习·算法
澜兮子1 小时前
LVS -DR
linux·服务器·lvs
wxjlkh3 小时前
vSphere 7.0 client 提示HTTP状态 500- 内部服务器错误
服务器·windows·http
努力学习的小廉5 小时前
深入了解linux系统—— 库的制作和使用
linux·运维·单片机
444A4E6 小时前
深入解析 Linux 进程状态:从 task_struct 双链表到 R/S/D/Z 状态的内核奥秘
linux·操作系统
国际云6 小时前
腾讯云国际站性能调优
运维·服务器·数据库·云计算·腾讯云
黑风风7 小时前
Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)
linux·ubuntu·postgresql