Linux开机自启动脚本

590 阅读1分钟

1.chkconfig

1.1 准备脚本

首先需要确保脚本前三行的内容必须如下格式:

#!/bin/bash
#chkconfig: 2345 80 90
#description: redis

脚本第一行 “#!/bin/bash” 告诉系统使用的shell;

脚本第二行 “#chkconfig: 2345 80 90” 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90);

脚本第三行 表示的是服务的描述信息;

注意: 第二行和第三行必写,负责会出现如“服务xxx不支持chkconfig”这样的错误。

将写好的脚本移动到/etc/rc.d/init.d/目录下,并给脚本赋可执行权限:

chmod +x xxx
cp xxx /etc/rc.d/init.d/

1.2 添加到启动项

添加脚本到开机自动启动项目中:

chkconfig --add xxx
chkconfig xxx on

1.3 启动和停止

这样以后便可以通过systemctl工具对脚本服务进行管理。

查看状态:

systemctl status xxx

启动服务:

systemctl start xxx

停止服务:

systemctl stop xxx

1.4 删除启动项

chkconfig xxx off
chkconfig --del xxx
rm -f /etc/rc.d/init.d/xxx