在 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 服务,它提供更精细的控制和日志跟踪。

相关推荐
plmm烟酒僧4 小时前
使用 Tmux 在断开SSH连接后,保持会话的生命周期
运维·ssh·tmux·分离会话
多多*6 小时前
Java设计模式 简单工厂模式 工厂方法模式 抽象工厂模式 模版工厂模式 模式对比
java·linux·运维·服务器·stm32·单片机·嵌入式硬件
南鸳6108 小时前
Linux常见操作命令(2)
linux·运维·服务器
fengwuJ8 小时前
Linux安装Idea
linux·ubuntu·debian·intellij-idea
Kaede68 小时前
怎么安装JSON服务器?JSON服务器最新安装教程
运维·服务器·json
西北大程序猿9 小时前
linux进程信号 ─── linux第27课
linux·运维·服务器·信号处理
涛ing9 小时前
【Git “fetch“ 命令详解】
linux·c语言·c++·人工智能·git·vscode·svn
想躺在地上晒成地瓜干9 小时前
树莓派超全系列文档--(18)树莓派配置音频
linux·音视频·树莓派·raspberrypi·树莓派教程
inxunoffice9 小时前
批量给 PDF 添加或删除密码保护,支持设置打开密码、只读密码、限制复制和打印
运维·服务器·pdf
宅小海10 小时前
14 配置Hadoop集群-配置历史和日志服务
linux·服务器·hadoop