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

相关推荐
我命由我123451 小时前
Windows 操作系统 - Windows 设置始终使用 Windows 照片查看器打开图片
运维·windows·经验分享·笔记·学习·操作系统·运维开发
乌萨奇也要立志学C++2 小时前
【Linux】linux基础开发工具(三) 版本控制器Git、调试器 - gdb/cgdb使用、一些实用的调试技巧
linux·git·elasticsearch
Ronin3052 小时前
【Linux系统】进程间通信:匿名管道
linux
Tipriest_2 小时前
ubuntu apt安装与dpkg安装相互之间的关系
linux·运维·ubuntu·apt·flatpak·dpkg·snap
liliangcsdn2 小时前
linux pip/conda 修改默认cache位置
linux·运维·pip
sky_share3 小时前
嵌入式相关书籍
linux
mortimer3 小时前
记一次网站服务器无缝扩容:利用软链接实现零停机数据迁移
linux·运维
Dontla3 小时前
docker desktop入门(docker桌面版)(提示wsl版本太低解决办法)
运维·docker·容器
gnawkhhkwang3 小时前
io_setup系统调用及示例
linux·c语言
没有bug.的程序员4 小时前
《Spring Boot应用工程化提升:多模块、脚手架与DevTools》
java·运维·spring boot