centos8下启用rc-local服务
一,centos8不建议写rc.local,默认启动时执行的命令放到何处?
以前我们会把linux开机执行的命令写入到/etc/rc.local
在centos8上系统不再建议我们写入到rc.local
[root@yjweb log]# more /etc/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file.
rc.local里面的comment说了,建议创建systemd service
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/20/centos8linux-qi-yong-rclocal-fu-wu/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,看一下系统默认/etc/rc.local的情况
[root@yjweb log]# ll /etc/rc.local lrwxrwxrwx 1 root root 13 Jul 1 2019 /etc/rc.local -> rc.d/rc.local [root@yjweb log]# ll /etc/rc.d/rc.local -rw-r--r--. 1 root root 474 Jul 1 2019 /etc/rc.d/rc.local
说明:可以看到:当前的/etc/rc.local是 /etc/rc.d/rc.local文件的一个符号链接
三,配置rc-local服务
我们在这里配置一个例子:开机后自动关闭透明大页内存:transparent_hugepage
1,编辑/etc/rc.d/rc.local文件
[root@yjweb log]# vi /etc/rc.d/rc.local
增加一行:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
2,加入可执行属性
[root@yjweb log]# chmod +x /etc/rc.d/rc.local [root@yjweb log]# ll /etc/rc.d/rc.local -rwxr-xr-x 1 root root 530 Mar 11 14:44 /etc/rc.d/rc.local
3,配置rc.local服务
[root@yjweb log]# vi /usr/lib/systemd/system/rc-local.service
内容如下:
[Unit] Description=/etc/rc.d/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.d/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.d/rc.local start TimeoutSec=0 RemainAfterExit=yes GuessMainPID=no [Install] WantedBy=multi-user.target
说明:最后的install一段不可少,
如果当前的service文件中没有这段,需手动添加
否则服务启动时会报错
3,启动
[root@yjweb log]# systemctl daemon-reload
[root@yjweb log]# systemctl start rc-local
4,使开机能自动启动:
[root@yjweb log]# systemctl enable rc-local
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /usr/lib/systemd/system/rc-local.service.
四,测试:换一台机器测试重启后是否可自动生效
先查看未配置前的系统默认设置
[root@localhost ~]# more /sys/kernel/mm/transparent_hugepage/enabled [always] madvise never
做完对rc.local的配置后重启机器
[root@localhost ~]# reboot
机器启动后查看,说明rc-local服务起作用了
[root@localhost liuhongdi]# more /sys/kernel/mm/transparent_hugepage/enabled always madvise [never]
五,查看本地centos的版本
[root@yjweb ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)