Linux中常见几种自启动方式的区别

在Linux中,有多个机制可以实现开机自启动,它们在不同的初始化系统(如SysVinit、systemd)下有不同的表现。以下是一些常见的自启动方法及其区别:

/etc/rc.local:

这是传统的SysVinit系统中的自启动脚本。在系统启动到指定运行级别时,会执行这个文件中的命令。

在使用systemd的系统中,为了兼容性,通常有一个rc-local.service来运行/etc/rc.local。但需要确保该服务被启用。

使用rc.local比较简单,只需将需要开机自启动的命令写入该文件即可。但注意,该文件默认可能没有执行权限,需要确保其有执行权限。

具体操作方式

1. 创建或编辑rc.local文件

sudo vim /etc/rc.local

2. 添加内容

写入自启动脚本

bash 复制代码
#!/bin/bash
sudo -u pharmacy /home/services/rcs/start_rcs.sh &
sudo -u pharmacy /home/abc.sh &
sudo -u pharmacy /home/edf.sh &

# 添加日志以便调试
echo "$(date): rc.local executed successfully" >> /var/log/rc-local.log
exit 0
3. 给文件添加执行权限
bash 复制代码
sudo chmod +x /etc/rc.local
4. 启用rc-local服务(如果尚未启用)

启用rc-local服务:、

bash 复制代码
sudo systemctl enable rc-local

启动服务:

bash 复制代码
sudo systemctl start rc-local

在这里如果提示如下,说明rc-local没有被systemctl管理到,那么需要新增service配置

bash 复制代码
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.

Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.
5.新增service 配置
bash 复制代码
sudo tee /etc/systemd/system/rc-local.service > /dev/null << 'EOF'
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF
6. 重新加载 systemd 并启用服务
bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable rc-local.service
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
6. 验证rc-local服务是否启用
bash 复制代码
sudo systemctl status rc-local.service

如果服务状态显示为active 则表示服务已经运行,下方会显示脚本的执行打印日志和时间

/etc/init.d/(SysVinit脚本):

在SysVinit系统中,每个运行级别都有对应的目录(如/etc/rc0.d~/etc/rc6.d),这些目录中的符号链接指向/etc/init.d/中的脚本。

可以通过update-rc.d(在Debian/Ubuntu中)或chkconfig(在RedHat/CentOS中)来管理这些链接,从而控制服务在哪个运行级别启动或停止。

这种方式较为传统,现在逐渐被systemd取代。

systemd:

现代大多数Linux发行版使用systemd作为初始化系统。

用户可以通过创建自定义的service单元文件(通常放在/etc/systemd/system/目录下)来管理自启动服务。

使用systemctl enable service_name来启用自启动,使用systemctl start service_name来立即启动服务。

systemd提供了更强大的功能,如依赖管理、条件启动、资源控制等。

使用方式

下方例子是启动指定脚本,然后在脚本中执行程序启动,不监视程序是否停止运行且不会重启,目标只是为了实现简单的自动启动。另外修改service内容,可以支持自启动和崩溃重启。

rcs.service 文件内容(只做开启自启动,不做程序退出检测和退出后的后的自启动)

bash 复制代码
[Unit]
Description=run jd rcs
After=network.target

[Service]
Type=oneshot
ExecStart=/home/services/rcs/start_rcs.sh
User=iamuser
Group=iamuser
WorkingDirectory=/home/prod/rcs/build/
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  • Type=oneshot:适用于只执行一次然后退出的服务
  • 移除了 ExecStop:因为 oneshot 类型不需要停止脚本
  • 保留 RemainAfterExit=yes:让 systemd 知道服务已"激活",即使主进程已退出

cron:

使用@reboot选项,可以在系统启动时运行指定的命令或脚本。

例如,在crontab中添加@reboot /path/to/script。

具体为:

bash 复制代码
crontab -e

添加以下行

bash 复制代码
@reboot /path/to/your/script.sh

这种方法简单,但不适合需要复杂依赖关系或需要作为守护进程运行的服务。

/etc/rc.d/rc.local(在某些发行版中):

类似于/etc/rc.local,在一些传统的发行版中(如RedHat/CentOS 6及以下)使用。

~/.config/autostart(桌面环境自启动):

对于图形界面环境,用户可以在~/.config/autostart目录下放置.desktop文件,以便在用户登录时自动启动应用程序。

/etc/xdg/autostart(系统级别的桌面自启动):

与用户级别的自启动类似,但是应用于所有用户。

区别总结:

适用范围:rc.local和SysVinit脚本通常用于系统级别的启动,而cron和桌面自启动可以用于用户级别。

灵活性:systemd提供了最灵活和强大的管理方式,可以处理复杂的依赖和条件。

兼容性:rc.local和SysVinit脚本在较老的系统中常见,而systemd是现代系统的标准。

使用简便性:对于简单的启动任务,rc.local和cron的@reboot可能更简单。

在选择自启动方法时,需要考虑你的发行版使用的初始化系统以及你的具体需求(如是否需要在图形界面启动后运行、是否需要守护进程等)。

在这里插入图片描述

推荐使用建议

  • 系统服务:使用Systemd(现代标准)
  • 简单个人任务:使用cron @reboot或rc.local
  • GUI应用:使用桌面环境自启动
  • 兼容性考虑:rc.local在大多数系统仍可用

选择哪种方式取决于你的具体需求、系统版本和使用场景。

相关推荐
上海蓝色星球5 小时前
迈向智慧电网新纪元:上海蓝色星球数字孪生变电主子站系统
运维·数据库
南棱笑笑生5 小时前
20251217给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-5.10】后调通ov5645【只能预览】
linux·c语言·开发语言·rockchip
爬山算法7 小时前
Netty(10)Netty的粘包和拆包问题是什么?如何解决它们?
服务器·网络·tcp/ip
Sleepy MargulisItG7 小时前
【Linux网络编程】应用层协议:HTTP协议
linux·服务器·网络·http
logic_57 小时前
静态路由配置
运维·服务器·网络
G31135422737 小时前
Linux 内核设计中的核心思想与架构原则
linux·架构·php
zhuzewennamoamtf7 小时前
Linux内核platform抽象、数据结构、内核匹配机制
linux·运维·数据结构
门思科技7 小时前
企业级 LoRaWAN 网关远程运维方案对比:VPN 与 NPS FRP 的技术与安全差异分析
运维·网络·安全
云和数据.ChenGuang7 小时前
Deepseek 持续迭代的模型
运维·运维技术·数据库运维工程师·运维教程
物联网软硬件开发-轨物科技8 小时前
【轨物方案】聚焦锯床设备智能化升级,打造工业互联网新范式
运维·科技·物联网