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

你可能感兴趣的:(运维测试,Java和Jvm)