环境是 CentOS8.4-2105
要开机自动运行自己写的服务脚本,操作如下,亲测可行:
1、首先切换到/etc/init.d目录下
# cd /etc/init.d
2、拷贝要运行的脚本文件到此目录,这个脚本是startMyServer.sh,现在是放在/root/目录下
# cp /root/startMyServer.sh /etc/init.d
脚本文件startMyServer.sh内容如下:
#!/bin/bash
# chkconfig: - 85 15
# description:开机自启脚本
cd /myprjs/task_zrtg_dj3
./serve.sh start
3、为这个脚本加上可执行权限
# chmod +x startMyServer.sh
4、至此,可将此脚本加入chkconfig命令进行管理
# chkconfig --add startMyServer.sh
5、设置该脚本为开机自运行
# chkconfig startMyServer.sh on
OK完成,现在该脚本在机器启动后自动运行
若要移除该自启动脚本,依次执行如下两句即可
# chkconfig startMyServer.sh off
# chkconfig --del startMyServer.sh
chkconfig --add 之后也可以用CentOS8系统自带的systemctl 命令查看和停止该脚本
# systemctl start startMyServer.service #开启该脚本
# systemctl restart startMyServer.service #重启动脚本
# systemctl stop startMyServer.service #停止该脚本
# systemctl status startMyServer.service #查看服务状态
亲测在CentOS8.4上可行!