在 CentOS 系统中开机自动执行 Shell 脚本

在 CentOS 系统中,可以通过以下方法设置开机自动执行 Shell 脚本。推荐使用 systemd 服务 (现代 Linux 系统的标准方式),也可以使用传统的 /etc/rc.local 方法。


方法 1:使用 Systemd 服务(推荐)

1. 创建 Systemd 服务文件

创建一个新的服务单元文件(例如 custom-script.service):

bash 复制代码
sudo vi /etc/systemd/system/custom-script.service
2. 编写服务配置

将以下内容粘贴到文件中,按需修改 ExecStartDescription

ini 复制代码
[Unit]
Description=Custom Startup Script
After=network.target

[Service]
Type=simple
ExecStart=/path/to/your/script.sh
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  • 关键参数说明
    • ExecStart: Shell 脚本的绝对路径(必须确保脚本有执行权限)。
    • After=network.target: 确保脚本在网络就绪后执行。
    • RemainAfterExit=yes: 脚本执行完成后,服务状态保持为"active"。
3. 设置脚本权限
bash 复制代码
# 赋予脚本可执行权限
sudo chmod +x /path/to/your/script.sh
4. 启用并启动服务
bash 复制代码
# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 启用开机自启动
sudo systemctl enable custom-script

# 启动服务(可选,测试脚本)
sudo systemctl start custom-script

# 检查服务状态
systemctl status custom-script

方法 2:使用 /etc/rc.local(传统方式)

如果系统支持 rc.local,可按以下步骤操作:

1. 创建或编辑 rc.local 文件
bash 复制代码
sudo vi /etc/rc.local
2. 添加脚本执行命令

在文件中添加以下内容(确保脚本路径正确):

bash 复制代码
#!/bin/bash
/path/to/your/script.sh
exit 0
3. 赋予 rc.local 执行权限
bash 复制代码
sudo chmod +x /etc/rc.local
4. 启用 rc-local 服务(如果未启用)
bash 复制代码
# 检查是否已存在 rc-local 服务
systemctl status rc-local

# 如果不存在,创建服务文件
sudo vi /etc/systemd/system/rc-local.service

粘贴以下内容:

ini 复制代码
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
5. 启用并启动服务
bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable rc-local
sudo systemctl start rc-local

验证和注意事项

  1. 脚本测试

    • 手动运行脚本确认无误:

      bash 复制代码
      sudo /path/to/your/script.sh
  2. 日志查看

    • 对于 systemd 服务,使用以下命令查看日志:

      bash 复制代码
      journalctl -u custom-script -f
  3. 脚本权限和路径

    • 确保脚本使用绝对路径,避免依赖环境变量。
    • 建议将脚本放在 /usr/local/bin/ 或专用目录(如 /opt/scripts/)。
  4. 依赖顺序

    • 如果脚本依赖其他服务(如网络、数据库),在 [Unit] 中添加 After=network.target mysqld.service

常见问题排查

  • 脚本未执行

    • 检查脚本权限:chmod +x /path/to/script.sh
    • 检查 systemd 服务状态:systemctl status custom-script
    • 检查日志:journalctl -u custom-script
  • /etc/rc.local 不生效

    • 确认系统支持 rc.local(CentOS 7+ 默认不启用,需手动配置)。
  • 脚本环境问题

    • 在脚本中指定 Shell 解释器(如 #!/bin/bash)。
    • 避免依赖交互式环境变量,使用绝对路径。

通过以上方法,Shell 脚本会在系统启动时自动执行。推荐优先使用 systemd 服务,它提供更精细的控制和日志跟踪。

相关推荐
傻傻虎虎14 分钟前
【Docker】常用帮忙、镜像、容器、其他命令合集(1)
运维·docker·容器
被遗忘的旋律.19 分钟前
Linux驱动开发笔记(十)——中断
linux·驱动开发·笔记
草履虫建模38 分钟前
在 RuoYi 中接入 3D「园区驾驶舱」:Vue2 + Three.js + Nginx
运维·开发语言·javascript·spring boot·nginx·spring cloud·微服务
wanhengidc39 分钟前
高性价比云手机挑选指南
运维·网络·安全·游戏·智能手机
凡间客1 小时前
Linux防火墙-Firewalld
linux·运维·服务器
nnerddboy1 小时前
Linux嵌入式自学笔记(基于野火EBF6ULL):1.配置环境
linux·笔记·单片机·嵌入式硬件
Justin_191 小时前
Linux防火墙firewalld
大数据·linux·运维
皆过客,揽星河3 小时前
Linux上安装MySQL8详细教程
android·linux·hadoop·mysql·linux安装mysql·数据库安装·详细教程
青草地溪水旁3 小时前
Unix/Linux 系统中的 `writev` 系统调用
linux·unix·writev
彩虹海。3 小时前
密码到期导致ssh连接失败
运维·ssh