Centos8 设置开机自启动脚本

简介: Centos8 设置开机自启动脚本


在CentOS8 之前通过把需要开机执行的命令写入到/etc/rc.local 就解决了开机启动问题,但是从CentOS8开始写入到rc.local 将无法自动启动 需要设置 rc.local这个服务自启解决开机/etc/rc.local 中脚本不执行问题

[root@hostname]# more /etc/rc.local #!/bin/bashTHISFILEISADDEDFORCOMPATIBILITYPURPOSESItishighlyadvisabletocreateownsystemdservicesorudevrulestorunscriptsduringbootinsteadofusingthisfile.

看一下系统默认/etc/rc.local的情况

[root@hostname]# ll /etc/rc.local lrwxrwxrwx1rootroot13Jul12019/etc/rc.local->rc.d/rc.local[root@hostname]# ll /etc/rc.d/rc.local -rw-r--r--.1rootroot474Jul12019/etc/rc.d/rc.local

说明:可以看到当前的/etc/rc.local是 /etc/rc.d/rc.local文件的一个符号链接


配置rc-local服务

我们在这里配置一个例子:开机后自动在/tmp 创建一个txt 文档

1.编辑/etc/rc.local文件

[root@hostname]# vi /etc/rc.local

增加一行:

touch/tmp/123.txt


2.加入可执行属性

[root@hostname]# chmod +x /etc/rc.d/rc.local   #必不可少,不然后面rc-local 无法启动[root@hostname]# ll /etc/rc.d/rc.local -rwxr-xr-x1rootroot530Mar1114:44/etc/rc.d/rc.local


3. 配置rc.local服务

[root@hostname]# vi /usr/lib/systemd/system/rc-local.service

内容如下:

[Unit]Description=/etc/rc.d/rc.localCompatibilityDocumentation=man:systemd-rc-local-generator(8)ConditionFileIsExecutable=/etc/rc.d/rc.localAfter=network.target[Service]Type=forkingExecStart=/etc/rc.d/rc.localstartTimeoutSec=0RemainAfterExit=yesGuessMainPID=no[Install]WantedBy=multi-user.target

说明:最后的install一段不可少,如果当前的service文件中没有这段,需手动添加(默认有前两部分) 否则服务启动时会报错



4.启动rc-local服务

[root@hostname]# systemctl daemon-reload [root@hostname]# systemctl start rc-local


5.使开机能自动启动:

[root@hostname]# systememctl enable rc-local Createdsymlink/etc/systemd/system/multi-user.target.wants/rc-local.service/usr/lib/systemd/system/rc-local.service.


测试

source /etc/rc.local 看下是否自动创建txt 文件 ,如创建成功说明脚本(这里只是写了一个简单的命令代替)自身不存在问题,删除这个txt 文件 并重启机器;机器启动后查看文件是否存在,如存在说明rc-local服务配置正常


相关文章
|
7天前
|
安全 Linux 数据安全/隐私保护
centos如何将一般用户设置为超级权限
【4月更文挑战第17天】centos如何将一般用户设置为超级权限
25 1
|
3月前
|
Linux 数据安全/隐私保护
CentOS设置IP地址的方法
CentOS设置IP地址的方法
530 0
|
2月前
|
Linux
CentOS8设置网络镜像安装源
CentOS8设置网络镜像安装源
72 0
|
8天前
|
Java Shell 应用服务中间件
centos7_tomcat开机自启的shell脚本参考
centos7_tomcat开机自启的shell脚本参考
11 0
|
10天前
|
Shell
CentOS6.5自动化安装LAMP脚本
CentOS6.5自动化安装LAMP脚本
|
19天前
|
Linux
CentOS 设置静态 IP 配置
防止 CentOS 服务器的 IP 地址更改,可以设置静态 IP 配置,而不是依赖 DHCP(动态主机配置协议)分配 IP 地址。
21 7
|
24天前
|
Linux PHP
zabbix3.0在centos7环境下always_populate_raw_post_data的设置
zabbix3.0在centos7环境下always_populate_raw_post_data的设置
10 0
|
3月前
|
Linux
centos7实现磁盘挂载,解挂,开机自动挂载,解决挂载文件覆盖问题
centos7实现磁盘挂载,解挂,开机自动挂载,解决挂载文件覆盖问题
133 0
|
4月前
|
Linux Shell
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
104 0
|
4月前
|
Linux
开源日志平台GrayLog5.1.7 CentOS7一键安装脚本
开源日志平台GrayLog5.1.7 CentOS7一键安装脚本
86 1

热门文章

最新文章