A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区


现代Linux发行版中rc.local的再次开启



前言

现在很多Linux发行版中,已经不再使用Init作为进程和服务管理系统了,那么,现代的Linux发行版中,基本上都是Sytemd作为进程和服务管理系统,而且使用上更加规范。

但是,就会出现一个问题,就是过去的开机自动启动项目,就无法通过常规的Init.d的思路配置成功。如果,还依然需要使用常规的rc.local配置来做开机自启动就需要专门配置。实际上Sytemd本身是支持rc启动服务的,只是默认屏蔽了而已。其它的,诸如OpenRC实现思路略有不同,本教程只涉及Systemd启动rc.local。

开启步骤

配置rc-local.service服务文件
找到对应目录,并配置内容:
[Shell] 纯文本查看 复制代码
sudo vim /etc/systemd/system/rc-local.service


并确认内容:
[Shell] 纯文本查看 复制代码
[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


激活rc-local.service
[Shell] 纯文本查看 复制代码
sudo systemctl enable rc-local.service


添加启动服务

手工创建或者拷贝已有的/etc/rc.local,并赋予执行权限

[Shell] 纯文本查看 复制代码
#!/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


给予脚本执行权限

[Shell] 纯文本查看 复制代码
sudo chmod +x /etc/rc.local



总结


在现代Linux发行版中就可以采用以上方式开启 rc.local 自动启动。
您需要登录后才可以回帖 登录 | 加入黑马