跳至主要內容

设置开机自启动

三思原创大约 2 分钟Linuxlinux-设置开机自启动系统启动定时任务脚本

本文介绍了在Linux系统中设置开机自启动的三种方法:使用systemctl enable命令、编辑/etc/rc.local文件并添加需要执行的内容、使用chkconfig命令创建可执行脚本实现开机自启动。每种方法都有详细步骤和示例,适用于不同场景下的开机自启需求。

image
image

设置开机自启动

(11条消息) Linux设置开机自启_小基基o_O的博客-CSDN博客_linux开机自启open in new window

方法1:systemctl enable

启用 开机自动启动 的命令

systemctl enable

关闭 开机自动启动 的命令

systemctl disable

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

systemctl enable mysqld

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

方法2:/etc/rc.local

/etc/rc.local是rc.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,所以3是on其它是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