系统环境:centos7.9
方法一:
1. 编写开机自启脚本:/etc/init.d/autostart.sh
(注意:脚本中必须加入:
#!/bin/bash
#chkconfig:2345 10 90
#description:resind
其中 2345 是默认启动级别,级别有 0 - 6 共 7 个级别。等级 0 表示:表示关机
等级 1 表示:单用户模式
等级 2 表示:无网络连接的多用户命令行模式
等级 3 表示:有网络连接的多用户命令行模式
等级 4 表示:不可用
等级 5 表示:带图形界面的多用户模式
等级 6 表示:重新启动
10 是启动优先级,90 是停止优先级,优先级范围是 0-100,数字越大,优先级越低
2. 给脚本执行权限:
chmod a+x /etc/init.d/autostart.sh
3. 加入开机启动, 进 /etc/init.d/ 目录下:
chkconfig --add autostart.sh
chkconfig autostart.sh on
查看 wei.sh 开机自启动情况:chkconfig --list wei.sh
(注意非加入自启动而是手动启动这样:/etc/init.d/autostart.sh start)
方法二:
1. 编写启动脚本:/etc/init.d/autostart.sh
2. 给脚本执行权限:
chmod a+x /etc/init.d/autostart.sh
3. 写入到 /etc/rc.local 开机自启动文件里:
echo "/etc/init.d/autostart.sh" >> /etc/rc.local
4. 授予 /etc/rc.d/rc.local 文件执行权限
chmod a+x /etc/rc.d/rc.local
重启服务器,查看脚本是否启动
注意:/etc/rc.d/rc.local 脚本执行,在 /etc/profile 之前,若 /etc/rc.d/rc.local 用到 /etc/profile 的环境变量,Shell 无法执行成功
方法三:
1. 任意路径编写启动脚本:例如 /opt/service/bin/autostart.sh
2. 给脚本执行权限:
chmod a+x /opt/service/bin/autostart.sh
3. 编写 systemd 启动文件:例如 redis.serivce
vi /usr/lib/systemd/system/xxx.service #xxx 该服务名自定义
[Unit]
Description=xxx-server
After=network.target
[Service]
Type=simple
ExecStart=/opt/service/bin/autostart.sh
ExecStop=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
4. 重载 systemd 服务
systemctl daemon-reload
5. 利用 systemctl 命令来管理服务启动、开机自启、关闭等功能
systemctl start xxx
systemctl enable xxx #对应之前的服务 xxx 服务名
微信扫描下方的二维码阅读本文
正文完