Linux 系统中没有 rc.local 文件的解决方法

我们知道 rc.local 文件中可以配置一些开机启动的程序,目前比较新的系统,建议大家以服务的方式来配置开机启动,所以没有 rc.local 这个文件了,本教程跟大家分享如何创建 rc.local,并设置程序的开机启动。在 Debian 11 下测试可行,其他的 Linux 发行版应该也可行。

1、在 /etc/systemd/system/ 目录下新建 rc-local.service 文件,使用 vim 编辑器,复制粘贴下面的内容:

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

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

[Install]
WantedBy=multi-user.target

2、在 /etc/ 目录下新建 rc.local 文件,使用 vim 编辑器,复制粘贴下面的内容:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.
# 可以在 exit 0 之前添加需要开机启动的程序
# ./root/run/kms &
exit 0

示例中我们中可执行的程序后面加了一个 & ,那是因为 rc.local 也是一个脚本,脚本运行时必须能够返回或者可以在后台运行,如果在这个脚本里面执行了一些死循环或者其他无法返回的任务,整个系统就很可能卡死在这里,无法启动,& 的作用就是让程序在后台运行。

3、赋予权限,激活 rc-local 服务

chmod +x /etc/rc.local

systemctl enable rc-local.service

4、检验是不是已经开机启动

在 Linux 系统重启后,可以使用 ps 命令查看程序是不是已经开机启动:

ps -ef | grep kms

使用 rc.local 创建 kms 进程,并设置开机启动。

kms 是查找的关键字,根据自己需要开机的程序进行查找。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>