linux把jar做成服務

1、創建開機啓動文件

touch /etc/rc.d/init.d/stm-nec

2、vi /etc/rc.d/init.d/tm-nec

3、文件內容如下

#!/bin/bash
#chkconfig: 2345 10 90
#description:stm-nec
BASE_DIR="/home/stm/jar/" 
JAR_FILE="stm-nec.jar"
SERVICE_NAME="stm-nec"
 
start()  
{  
echo "starting ${SERVICE_NAME}..."
cd $BASE_DIR
nohup java -jar $JAR_FILE > /home/stm/jar/${SERVICE_NAME}.log &
echo "${SERVICE_NAME} started"
}  
 
stop()  
{  
echo "stopping ${SERVICE_NAME}..."  
pid=`ps -ef|grep $JAR_FILE |grep -v grep |awk '{print $2}'`
kill -9 $pid 
echo "${SERVICE_NAME} stopped"
}
 
case "$1" in
 
start)  
start 
;;
 
stop)  
stop 
;; 
 
restart)  
stop  
start  
;;
 
*) 
echo "Usage: `basename $0` start|stop|restart"
esac 
exit 0

 

4、賦予權限

chmod +x /etc/rc.d/init.d/stm-nec

5、設置開機啓動

chkconfig --add stm-nec

6、啓動服務

systemctl start stm-nec

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章