centos7使用Shell脚本设置开机自启

centos7使用Shell脚本设置开机自启

一、创建shell脚本文件

touch /etc/init.d/AutoRunJava.sh

二、编写shell脚本

#!/bin/bash
#chkconfig: 35 20 80
#description: http server


export datacollect=/appdata/data_collection_analyze_server/aoya_data_collection_analyze_server-0.0.1-SNAPSHOT.jar
export datapush=/appdata/datapush/aoya_datapush-0.0.1-SNAPSHOT.jar
export aoya=/appdata/aoya/aoya-1.5.jar
 
export datacollect_port=8345
export datapush_port=8877
export aoya_port=7776
 
echo "--------data_collect开始启动--------------"
nohup java -jar $datacollect > /dev/null 2>&1 &
datacollect_port=`lsof -i:$datacollect_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "datacollect_port" ]
            do
              datacollect_port=`lsof -i:$datacollect_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "datacollect_port pid is $datacollect_port" 
echo "--------datacollect_port 启动成功--------------"


echo "--------datapush开始启动--------------"
nohup java -jar $datapush > /dev/null 2>&1 &
datapush_pid=`lsof -i:$datapush_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$datapush_pid" ]
            do
              datapush_pid=`lsof -i:$datapush_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "datapush pid is $datapush_pid" 
echo "--------datapush 启动成功--------------"



echo "--------aoya开始启动--------------"
nohup java -jar $aoya > /dev/null 2>&1 &
aoya_pid=`lsof -i:$aoya_port|grep "LISTEN"|awk '{print $2}'`
until [ -n "$tongda_pid" ]
            do
              tongda_pid=`lsof -i:$aoya_port|grep "LISTEN"|awk '{print $2}'`  
            done
echo "aoya pid is $aoya_pid" 

三、设置脚本的可执行权限

chmod u+x /etc/init.d/AutoRunJava.sh

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

cd /etc/init.d
chkconfig --add  AutoRunJava.sh

五、查看是否执行成功:

chkconfig --list AutoRunJava.sh

AutoRunJava.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off

表示已生效

如果0-6全为off则表示未生效,此时执行下面的命令:

chkconfig AutoRunJava.sh on

六、reboot重启

你可能感兴趣的:(centos7使用Shell脚本设置开机自启)