方法1:systemctl enable

启用 开机自动启动 的命令

systemctl enable

关闭 开机自动启动 的命令

systemctl disable

示例:设置MySQL开机自动启动

systemctl enable mysqld

备注:该方法不是所有场景都能用

方法2:/etc/rc.local

/etc/rc.localrc.d/rc.local的软链

1、编辑文件

vim /etc/rc.local

2、添加需要执行的内容

date > /root/a.txt

3、添加可执行权限

chmod 777 /etc/rc.local

4、重启

reboot

5、查看是否执行了开机自启命令

cat a.txt

方法3:chkconfig

1、在/etc/init.d/下创建可执行.sh文件

touch /etc/init.d/tmp.sh
chmod 777 /etc/init.d/tmp.sh
vim /etc/init.d/tmp.sh

2、写入内容(#chkconfig是必须的,3个参数代表:运行级别,启动优先权,关闭优先权)

#!/bin/sh
#chkconfig: 3 25 75

date >> /root/t.txt

3、添加

chkconfig --add /etc/init.d/tmp.sh

4、查看是否添加

chkconfig

上面脚本内容设置了#chkconfig的运行级别是3,所以3on其它是off

--level levels
Specifies  the run levels an operation should pertain to.
It is given as a string of numbers from 0 to 6.
For example, --level 35 specifies runlevels 3 and 5.
运行级别说明
0关机
1单用户【找回丢失的密码】 如果忘记root登陆密码,可以使用单用户登陆实现密码找回
2多用户状态没有网络服务
3多用户状态有网络服务
4系统未使用保留给用户
5图形界面
6系统重启

5、重启验证

reboot
cat /root/t.txt

6、删除

chkconfig --del /etc/init.d/tmp.sh

应用场景

CDH的工作节点重启后,服务会重启
然而有些服务不会重启,例如DolphinScheduler、Superset等
对此添加Superset开机自启

chmod 777 /etc/rc.local
vim /etc/rc.local
/home/yellow/bin/superset.sh start

爱的天灵灵

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐