Linux 系统服务配置指南(以 Kangle 为例)及 rc.local 恢复方法
一、创建自定义系统服务(以 Kangle 开机自启为例)
通过 systemd
配置系统服务,可实现程序的开机自启、状态管理与便捷控制,以下是完整步骤:
1. 新建服务配置文件
系统服务文件统一存放在 /lib/systemd/system/
目录,文件需以 .service
为后缀。以 Kangle 服务为例:
vi /lib/systemd/system/kangle.service
按 i
进入编辑模式,粘贴以下内容(需确保命令路径与实际环境一致):
[Unit]
Description=Kangle Web Service # 服务描述,自定义名称
After=network.target # 服务启动依赖:确保网络服务启动后再运行Kangle
[Service]
Type=forking # 启动类型:后台守护进程模式(常用)
ExecStart=/vhs/kangle/bin/kangle # 服务启动命令(必须绝对路径)
ExecReload=/vhs/kangle/bin/kangle --reboot # 服务重载命令(重启配置)
ExecStop=/vhs/kangle/bin/kangle -q # 服务停止命令(必须绝对路径)
[Install]
WantedBy=multi-user.target # 服务安装目标:多用户模式下生效(常规服务默认)
按 Esc
退出编辑,输入 :wq!
保存并退出。
2. 核心配置参数说明(理解服务逻辑)
(1)[Unit] 段:服务基础信息与依赖
Description
:服务的简短描述,便于识别(如Kangle Web Service
)。After
:定义服务启动顺序,避免依赖服务未就绪导致启动失败(如network.target
表示“网络就绪后启动”)。
(2)[Service] 段:服务运行核心配置
Type(启动类型):根据程序运行方式选择,常见类型如下:
Type=simple
(默认):程序启动后不 fork 子进程,直接在前台运行(适合非守护进程)。Type=forking
(推荐用于守护进程):程序启动后会 fork 子进程,父进程退出后视为启动成功(如 Kangle、Nginx 等后台服务),建议配合PIDFile=
配置(指定进程ID文件路径,便于 systemd 跟踪主进程)。Type=oneshot
:程序仅执行单次任务后退出(如初始化脚本),需搭配RemainAfterExit=yes
让 systemd 认为服务持续激活。Type=notify
:程序启动就绪后会主动向 systemd 发送信号(需程序支持,如部分现代服务)。Type=dbus
:服务通过 DBus 总线启动,需指定BusName=
(如桌面环境服务)。
ExecStart/ExecReload/ExecStop
:分别对应“启动、重载、停止”命令,必须使用绝对路径(避免因环境变量问题导致命令失效)。可选参数:
PrivateTmp=True
:给服务分配独立临时目录(增强安全性)。User/Group
:指定服务运行的用户/用户组(避免用 root 运行非必要服务)。WorkingDirectory
:服务的工作目录(程序依赖相对路径时配置)。RestartSec
:服务异常退出后,重启前的延迟时间(如RestartSec=3
表示延迟3秒重启)。
(3)[Install] 段:服务安装配置
WantedBy=multi-user.target
:表示服务在“多用户命令行模式”下生效(常规服务器场景默认);若为桌面服务,可改为WantedBy=graphical.target
(图形界面模式)。
3. 服务管理常用命令(操作服务)
功能需求 | 命令 |
---|---|
设置开机自启 | systemctl enable kangle.service |
取消开机自启 | systemctl disable kangle.service |
启动服务 | systemctl start kangle.service |
停止服务 | systemctl stop kangle.service |
重启服务 | systemctl restart kangle.service |
重载服务配置(改.service文件后) | systemctl daemon-reload |
查看服务详细状态 | systemctl status kangle.service |
仅判断服务是否激活 | systemctl is-active kangle.service |
查看所有已启动的服务 | systemctl list-units --type=service |
示例操作(Kangle 服务初始化):
# 重载配置(若刚创建/修改.service文件)
systemctl daemon-reload
# 启动服务
systemctl start kangle.service
# 设置开机自启
systemctl enable kangle.service
# 查看服务状态(确认是否正常运行)
systemctl status kangle.service
二、恢复 Debian 8.11.1+ 缺失的 rc.local 文件
部分新版本 Linux 发行版(如 Debian 8.11.1 及以上、Ubuntu 16.04+)默认移除了 /etc/rc.local
文件,若需使用传统 rc.local
执行开机脚本,可按以下步骤恢复:
1. 创建 rc-local.service 系统服务
vi /lib/systemd/system/rc-local.service
按 i
进入编辑模式,粘贴以下内容(Debian 8.11.1 兼容版本):
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility # 服务描述:兼容rc.local
ConditionFileIsExecutable=/etc/rc.local # 条件:仅当/etc/rc.local可执行时生效
After=network.target # 依赖:网络就绪后启动
[Service]
Type=forking # 启动类型:后台模式
ExecStart=/etc/rc.local start # 执行rc.local脚本
TimeoutSec=0 # 超时时间:无限制(避免脚本执行超时被终止)
RemainAfterExit=yes # 脚本退出后仍视为服务激活
SysVStartPriority=99 # SysV兼容优先级(低优先级,确保晚于其他服务)
按 Esc
,输入 :wq!
保存退出。
2. 创建 /etc/rc.local 脚本文件
vi /etc/rc.local
按 i
进入编辑模式,粘贴基础模板(脚本需以 #!/bin/sh -e
开头,末尾必须有 exit 0
):
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# 此处添加自定义开机脚本(示例:启动自定义程序)
# /path/to/your/script.sh
exit 0 # 必须保留,确保脚本正常退出
按 Esc
,输入 :wq!
保存退出。
3. 赋予执行权限并启用服务
# 给rc.local添加执行权限(否则无法运行)
chmod +x /etc/rc.local
# 启用rc-local服务(设置开机自启)
systemctl enable rc-local.service
# 启动服务并查看状态(确认是否正常)
systemctl start rc-local.service
systemctl status rc-local.service
4. 验证效果
重启系统后,通过以下命令确认 rc.local
中的脚本是否执行:
reboot # 重启系统
# 重启后检查服务状态
systemctl status rc-local.service
# 或查看系统日志(排查执行问题)
journalctl -u rc-local.service
注意事项
- 服务路径一致性:配置
ExecStart
等命令时,务必通过which 命令
确认绝对路径(如which kangle
查看 Kangle 可执行文件路径),避免路径错误导致服务启动失败。 - rc.local 兼容性:恢复的
rc.local
本质是通过systemd
服务运行,脚本中避免使用需要交互的命令(如read
),且需确保所有命令可在后台执行。 - 服务冲突排查:若服务启动失败,通过
systemctl status 服务名
查看错误日志,常见问题包括“路径错误”“权限不足”“依赖服务未启动”。 - 优先选择 systemd 服务:相比
rc.local
,systemd
服务支持状态管理、日志记录、依赖控制,更适合现代 Linux 系统,推荐优先使用。
评论区(1条评论)
欢迎加入 Typecho 大家族