linux服务器启动jar包程序

新建一个maven工程

pom中引入

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包启动可能找不到主类-->
                            <mainClass>com.demo.websocket.websocketspringboot.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <!--主要是使用goal来指定需要重新打包。
                            作用是在原始Maven打包形成的jar包基础上,进行重新打包,
                            新形成的jar包不但包含应用类文件和配置文件,
                            而且还会包含应用所依赖的jar包以及Springboot启动相关类,
                            以此来满足Springboot独立应用的特性。-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

1. jar包启动

java -jar jar包名

窗口被锁定, CTRL + C打断程序运行,或直接关闭窗口,程序退出

java -jar jar包名 &

&代表在后台运行。优点是当前ssh窗口不被锁定, enter键可以跳出锁定,但是当窗口关闭时,程序终止运行。

nohup java -jar jar包名

nohup java -jar websocket-springboot-1.0-SNAPSHOT.jar

nohup 意思是不挂断运行命令,当账户退出或终端关闭时,程序仍然运行。 当用 nohup 命令运行jar包时,缺省情况下该应用的所有输出被重定向到nohup.out的文件中, 然后,我们在当前目录下看到了nohup.out文件。,除非另外指定了输出文件。

nohup java -jar jar包名 > 文件名

nohup java -jar websocket-springboot-1.0-SNAPSHOT.jar > websocket-springboot.log

脚本启动

#!/bin/bash
 
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=websocket-springboot-1.0-SNAPSHOT.jar
 
 
#使用说明,用来提示输入参数
usage() {
    echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
    exit 1
}
 
#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk {print $2} `
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
 
#启动方法
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is already running. pid=${pid} ."
  else
    nohup java -Xmx512m -Xms512m -jar $APP_NAME  > test2.txt 2>&1 &
    echo "${APP_NAME} start success"
  fi
}
 
#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
  else
    echo "${APP_NAME} is not running"
  fi  
}
 
#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}
 
#重启
restart(){
  stop
  start
}
 
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac
经验分享 程序员 微信小程序 职场和发展