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在大多数系统仍可用

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

相关推荐
脏脏a35 分钟前
【Linux】Linux进程状态深度解析
linux·运维·服务器
小熊officer39 分钟前
Nginx学习
运维·学习·nginx
LCG元44 分钟前
考古利器:find 命令的高级用法,按时间、大小、内容精准查找
linux
ManThink Technology1 小时前
LoRaWAN网关:连接私有服务器是“可行”还是“明智”?
运维·服务器
t***82112 小时前
华为数据中心CE系列交换机级联M-LAG配置示例
服务器·华为·php
U***74692 小时前
Linux(CentOS)安装 MySQL
linux·mysql·centos
3***g2052 小时前
Linux系统离线部署MySQL详细教程(带每步骤图文教程)
linux·mysql·adb
J***Q2922 小时前
DevOps金融服务安全要求
运维·安全·devops
Dovis(誓平步青云)2 小时前
《内核视角下的 Linux 锁与普通生产消费模型:同步原语设计与性能优化思路》
linux·运维·性能优化