一、windows版
1.在idea中用maven将程序打成jar,放到目录中。
2.去github上面下载winsw: https://github.com/kohsuke/winsw/releases
3. 将WinSW.NET4.exe文件复制到java程序所在文件夹中
4.将java程序重命名,去掉名称中的“.”。例如hello-1.0.jar ----> hello.jar
5.将WinSW.exe重命名为hello.exe(和jar同名)
6. 新建一个xml文件,命名为hello.xml
<service>
<id>hello</id>
<name>hello</name>
<description>This is hello service.</description>
<!-- java环境变量 -->
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<arguments>-jar "E:\springboot\hello.jar"</arguments>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>E:\springboot\log</logpath>
<logmode>rotate</logmode>
</service>
7.命令行定位到当前目录,执行: hello.exe install
8. 去windows服务列表中启动程序。
二、linux版
1、创建新文件 touch /etc/rc.d/init.d/hello
2、编辑文件 vi /etc/rc.d/init.d/hello
3、编辑内容
#!/bin/bash
#chkconfig: 2345 10 90
#description:hello
BASE_DIR="/root/hello" #jar包存放的路径
JAR_FILE="hello.jar" #jar包名称
SERVICE_NAME="helloAp9000" #服务名称
start()
{
echo "starting ${SERVICE_NAME}..."
cd $BASE_DIR
nohup java -jar $JAR_FILE > /root/hello/hello.out 2>&1
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/hello
5、设置开机启动 chkconfig --add hello
6、启动服务 ./hello start
7、停止服务 ./hello stop