这是传统的SysVinit系统中的自启动脚本。在系统启动到指定运行级别时会执行这个文件中的命令。在使用systemd的系统中为了兼容性通常有一个rc-local.service来运行/etc/rc.local。但需要确保该服务被启用。使用rc.local比较简单只需将需要开机自启动的命令写入该文件即可。但注意该文件默认可能没有执行权限需要确保其有执行权限。具体操作方式1. 创建或编辑rc.local文件1sudovim/etc/rc.local2. 添加内容写入自启动脚本12345678#!/bin/bashsudo-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.logexit03. 给文件添加执行权限1sudochmodx/etc/rc.local4. 启用rc-local服务如果尚未启用启用rc-local服务、1sudosystemctlenablerc-local启动服务1sudosystemctl start rc-local在这里如果提示如下,说明rc-local没有被systemctl管理到那么需要新增service配置12345678910111213The unit files have no installation config (WantedBy, RequiredBy, Also,Alias settingsinthe [Install] section, and DefaultInstancefortemplateunits). This means they are not meant to be enabled using systemctl.Possible reasonsforhaving this kind ofunitsare:• A unit may be statically enabled by being symlinked from another units.wants/ or .requires/ directory.• A units purpose may be to act as a helperforsome other unitwhichhasa requirement dependency on it.• A unit may be started when needed via activation (socket, path, timer,D-Bus, udev, scripted systemctl call, ...).• Incaseof templateunits, the unit is meant to be enabled with someinstance name specified.5.新增service 配置123456789101112131415sudo tee /etc/systemd/system/rc-local.service /dev/null EOF[Unit]Description/etc/rc.local CompatibilityConditionFileIsExecutable/etc/rc.localAfternetwork.target[Service]TypeforkingExecStart/etc/rc.local startTimeoutSec0RemainAfterExityes[Install]WantedBymulti-user.targetEOF6. 重新加载 systemd 并启用服务1234sudosystemctl daemon-reloadsudosystemctlenablerc-local.servicesudosystemctl start rc-local.servicesudosystemctl status rc-local.service7. 验证rc-local服务是否启用1sudosystemctl 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 文件内容只做开启自启动不做程序退出检测和退出后的后的自启动