Linux如何创建初始化脚本_Linux开机执行shell脚本配置【自动化】
技术百科
穿越時空
发布时间:2026-01-19
浏览: 次 推荐使用systemd服务单元:创建可执行脚本,编写.service文件定义启动时机与依赖,启用并启动服务;其次可选rc.local、crontab @reboot或profile配置,但适用场景各异。
如果您希望在Linux系统启动时自动执行特定的Shell脚本,则需要将脚本注册为系统服务或配置到初始化流程中。以下是实现该目标的多种方法:
一、使用systemd服务单元(推荐用于现代发行版)
systemd是当前主流Linux发行版(如CentOS 7+、Ubuntu 16.04+、Debian 8+)默认的初始化系统,通过编写.service文件可精确控制脚本的启动时机、依赖关系与运行环境。
1、创建Shell脚本并赋予执行权限:
使用文本编辑器新建脚本(例如 /opt/scripts/boot_init.sh),以#!/bin/bash开头,写入所需命令;
执行 chmod +x /opt/scripts/boot_init.sh 确保其可执行。
2、创建systemd服务文件:
执行 sudo nano /etc/systemd/system/boot-init.service,写入以下内容:
[Unit]
Description=Boot-time initialization script
After=network.target
[Service]
Type=oneshot
ExecStart=/opt/scripts/boot_init.sh
RemainAfterExit=yes
User=root
[Install]
WantedBy=multi-user.target
3、启用并验证服务:
执行 sudo systemctl daem

执行 sudo systemctl enable boot-init.service;
执行 sudo systemctl start boot-init.service 测试立即运行;
执行 sudo systemctl status boot-init.service 查看输出与状态。
二、添加至/etc/rc.local(兼容传统SysVinit及部分systemd系统)
/etc/rc.local 是一个被多数Linux发行版保留的兼容性入口,systemd会将其作为legacy服务包装启动,适用于快速部署且无需复杂依赖管理的场景。
1、确认rc-local服务已启用:
执行 sudo systemctl is-enabled rc-local;若返回disabled,则需启用:
执行 sudo systemctl enable rc-local。
2、编辑rc.local文件:
执行 sudo nano /etc/rc.local;
在 exit 0 行之前插入:/opt/scripts/boot_init.sh >> /var/log/boot-init.log 2>&1;
确保第一行包含 #!/bin/bash(若文件尚无此行);
保存后执行 sudo chmod +x /etc/rc.local。
3、重启验证:
执行 sudo reboot;
启动完成后检查日志:cat /var/log/boot-init.log。
三、通过crontab @reboot指令触发
crontab的@reboot选项允许在每次系统引导完成用户级环境初始化后运行命令,不依赖系统服务管理器,适合非特权用户或轻量级任务。
1、编辑root用户的crontab:
执行 sudo crontab -e。
2、添加一行定时任务:
@reboot /opt/scripts/boot_init.sh >> /var/log/cron-boot.log 2>&1
3、保存退出后,cron守护进程将在下次启动时自动加载该条目;
注意:该方式在系统尚未完全就绪(如网络未通、NFS未挂载)时可能失败,务必在脚本内加入适当的等待或条件判断逻辑。
四、修改/etc/profile或/etc/bash.bashrc(仅限交互式登录Shell)
此方法仅影响用户登录时启动的Shell,不适用于纯后台服务或无人值守启动场景,但可用于调试阶段快速验证脚本逻辑。
1、选择全局配置文件:
若需所有用户生效,编辑 /etc/profile;
若仅影响bash Shell,可编辑 /etc/bash.bashrc。
2、在文件末尾追加执行语句:
if [ -f "/opt/scripts/boot_init.sh" ] && [ "$PS1" ]; then
/opt/scripts/boot_init.sh >> /var/log/profile-boot.log 2>&1
fi
3、该代码确保脚本仅在交互式Shell中运行,避免非终端环境(如scp、cron子shell)误触发;此方式不能替代真正的开机启动机制。
# 自动化
# ai
# 是一个
# 适用于
# 推荐使用
# 将在
# 启动时
# 如果您
# 配置文件
# 可执行
# 运行环境
# linux
# centos
# ubuntu
# linux系统
# debian
# if
# var
# 则需
# bash
# 发行版
# shell脚本
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- php怎么下载安装并配置环境变量_命令行调用PHP
- VSC怎样在VSC中调试PHPAPI_接口调试技巧
- 如何使用Golang sync.Map实现并发安全
- MAC如何修改默认应用程序_MAC文件后缀关联设置
- 如何使用Golang构建简易投票统计功能_Gola
- Win11文件扩展名怎么显示 Win11查看文件后
- Windows10怎样连接蓝牙设备_Windows
- 如何在Golang中配置代码格式化工具_使用gof
- Windows系统时间服务错误_W32Time服务
- Windows资源管理器总是卡顿或重启怎么办?(修
- Python lxml的etree和Element
- c++ nullptr与NULL区别_c++11空
- VSC怎么快速定位PHP错误行_错误追踪设置法【方
- Win11怎样安装企业微信_Win11安装企业微信
- Windows10系统怎么查看IP地址_Win10
- php增删改查在php8里有什么变化_新特性对cu
- 如何减少Golang内存碎片化_Golang内存分
- Golang如何遍历目录文件_Golang fil
- Mac怎么安装软件_Mac安装dmg与pkg文件的
- Mac怎么查看活动监视器_理解Mac进程和资源占用
- Win11怎么设置DNS服务器_Windows11
- c++ atoi和atof函数用法_c++字符数组
- Win11怎么修改DNS服务器 Win11设置DN
- 如何在 IIS 上为 ASP.NET 6 应用排除
- Win11如何连接Xbox手柄 Win11蓝牙连接
- Win11怎么更改计算机名_Windows11系统
- Win11怎么设置默认邮件应用_Windows11
- Windows怎样关闭Edge新标签页广告_Win
- MySQL 中使用 IF 和 CASE 实现查询字
- Win11怎么关闭自动调节亮度 Win11禁用内容
- php中常量能用::访问吗_类常量与作用域操作符使
- Win11关机界面怎么改_Win11自定义关机画面
- Win11怎么关闭自动调节屏幕亮度_Windows
- 如何使用Golang处理网络超时错误_Golang
- c++怎么使用std::filesystem遍历文
- php订单日志怎么按金额排序_php按订单金额排序
- php修改数据怎么改富文本_update更新htm
- 如何使用Golang实现聊天室消息存档_存储聊天记
- Windows10系统更新错误0x80070002
- php485函数执行慢怎么优化_php485性能提
- Windows10如何更改开机密码_Win10登录
- php删除数据怎么加限制_带where条件删除避免
- Win11快速助手怎么用_Win11远程协助连接教
- Linux如何安装Tomcat应用服务器_Linu
- Windows10如何查看保存的WiFi密码_Wi
- Windows如何拦截腾讯视频广告_Windows
- Windows10怎么查看系统激活状态_Windo
- php错误怎么开启_display_errors与
- c++如何实现多态性_c++ 虚函数表原理与动态绑
- Win11怎么更改输入法顺序_Win11调整语言首

QQ客服