CentOS8开机自运行shell脚本

首先切换到/etc/init.d目录下

cd /etc/init.d


新建shell脚本

vim test.sh


脚本文件test.sh内容如下:

#!/bin/sh
#chkconfig:2345 90 10
#description: 描述#下面写命令
cd /usr/local/test-server/bin
./start.sh

  

保存退出

为这个脚本加上可执行权限

chmod +x test.sh

至此,可将此脚本加入chkconfig命令进行管理

chkconfig --add test.sh

 

设置该脚本为开机自运行

 

chkconfig test.sh on

OK完成,现在该脚本在机器启动后自动运行

若要移除该自启动脚本,依次执行如下两句即可

chkconfig test.sh off

chkconfig --del test.sh

 补充:

设置centos开机启动执行sh脚本:
cd /etc/rc.d/init.d
chkconfig --add zgmsStart.sh
以下为例子
zgmsStart.sh
#!/bin/sh
#chkconfig:2345 99 10
#description: 描述#启动后台程序
source /etc/profile
cd /usr/local/zgms
./all-run.sh start
#!/bin/sh
以shell脚本方式执行
source /etc/profile
一般配置的java环境变量在启动时会失效,所以得有这刷新一下环境变量
#chkconfig:2345 80 90
2345指定脚本运行级别,对应系统的系统初始化启动服务目录 /etc/rc.d下的rc2.d,rc3.d,rc4.d,rc5.d
80为启动序号(数字越小执行顺序越靠前0-100),例如等同于在rc2.d目录下手动创建S80service-test服务
90为关闭序号(数字越小执行顺序越靠前0-100)

 

posted @ 2022-06-10 17:30  小哥的吃喝玩乐  阅读(1810)  评论(0编辑  收藏  举报