脚本
以下为shell脚本内容,在自己电脑新建QuickDeploy.sh文件,并将内容复制到sh文件中即可。
#!/bin/bash
#这里替换为你自己的执行程序(jar包)存放路径
APP_PATH=/home/admin
#这里替换为你自己的执行程序(jar包)文件名,启动方法中修改启动参数
APP_NAME=demo.jar
#这里替换为你自己的执行程序(jar包)的启动日志文件路径+文件名
APP_LOG_PATH=/home/admin/nohup.out
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh demo.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
echo "jar path: ${APP_PATH}"
cd $APP_PATH
nohup java -Xms128m -Xmx128m -jar $APP_NAME &
tail -f $APP_LOG_PATH
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
使用
准备程序对脚本进行测试:
package com.example.study;
import ...;
/**
* @Description: 启动类
* @Author Lenon Jin
* @Date 2021/8/25 10:39 下午
* @Version 1.0
*/
@SpringBootApplication
@Slf4j
public class StudyApplication {
public static void main(String[] args) {
log.info("窝窝头一块钱四个嘿嘿!");
SpringApplication.run(StudyApplication.class, args);
}
}
将shell文件上传至服务器,记得按照文件中注释修改jar包路径,jar包文件名,日志文件路径,文件信息:
root@iZbp12n8q:/home/admin# ll
total 32480
-rw-r--r-- 1 root root 1579 Aug 25 22:30 QuickDeploy.sh
-rw-r--r-- 1 root root 33234142 Aug 25 21:52 study-0.0.1-SNAPSHOT.jar
对QuickDeploy.sh文件赋予可执行权限,命令:chmod u+x QuickDeploy.sh
root@iZbp12n8q:/home/admin# chmod u+x QuickDeploy.sh
root@iZbp12n8q:/home/admin# ll
total 32480
-rwxr--r-- 1 root root 1596 Aug 25 22:51 QuickDeploy.sh*
-rw-r--r-- 1 root root 33234142 Aug 25 21:52 study-0.0.1-SNAPSHOT.jar
启动程序:./QuickDeploy.sh start
,此处可以看到我们在启动类中添加的日志,程序启动成功。
root@iZbp12n8q:/home/admin# ./QuickDeploy.sh start
jar path: /home/admin
nohup: appending output to 'nohup.out'
22:54:37.693 [main] INFO com.example.study.StudyApplication - 窝窝头一块钱四个嘿嘿!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.0)
2021-08-25 22:54:38.346 INFO 22040 --- [ main] com.example.study.StudyApplication : Starting StudyApplication v0.0.1-SNAPSHOT using Java 1.8.0_301 on iZbp12n8q with PID 22040 (/home/admin/study-0.0.1-SNAPSHOT.jar started by root in /home/admin)
2021-08-25 22:54:38.347 INFO 22040 --- [ main] com.example.study.StudyApplication : No active profile set, falling back to default profiles: default
2021-08-25 22:54:39.874 INFO 22040 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2021-08-25 22:54:39.886 INFO 22040 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-25 22:54:39.887 INFO 22040 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-08-25 22:54:39.952 INFO 22040 --- [ main] o.a.c.c.C.[.[localhost].[/study-server] : Initializing Spring embedded WebApplicationContext
2021-08-25 22:54:39.952 INFO 22040 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1530 ms
2021-08-25 22:54:40.908 INFO 22040 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path '/study-server'
2021-08-25 22:54:41.105 WARN 22040 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 22:54:41.110 WARN 22040 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 22:54:41.170 WARN 22040 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 22:54:41.196 INFO 22040 --- [ main] com.example.study.StudyApplication : Started StudyApplication in 3.327 seconds (JVM running for 3.75)
2021-08-25 22:54:41.197 INFO 22040 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-08-25 22:54:41.200 INFO 22040 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
输出程序运行状态:./QuickDeploy.sh status
root@iZbp12n8q:/home/admin# ./QuickDeploy.sh status
study-0.0.1-SNAPSHOT.jar is running. Pid is 22095
停止程序:./QuickDeploy.sh stop
root@iZbp12n8q:/home/admin# ./QuickDeploy.sh stop
root@iZbp12n8q:/home/admin# ./QuickDeploy.sh status
study-0.0.1-SNAPSHOT.jar is not running.
重启程序:./QuickDeploy.sh restart
,此命令前后调用了脚本的stop,start方法而已。
root@iZbp12n8q:/home/admin# ./QuickDeploy.sh restart
jar path: /home/admin
nohup: appending output to 'nohup.out'
23:02:01.681 [main] INFO com.example.study.StudyApplication - 窝窝头一块钱四个嘿嘿!
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.0)
2021-08-25 23:02:02.375 INFO 22252 --- [ main] com.example.study.StudyApplication : Starting StudyApplication v0.0.1-SNAPSHOT using Java 1.8.0_301 on iZbp12n8q with PID 22252 (/home/admin/study-0.0.1-SNAPSHOT.jar started by root in /home/admin)
2021-08-25 23:02:02.379 INFO 22252 --- [ main] com.example.study.StudyApplication : No active profile set, falling back to default profiles: default
2021-08-25 23:02:04.105 INFO 22252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2021-08-25 23:02:04.120 INFO 22252 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-25 23:02:04.121 INFO 22252 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-08-25 23:02:04.188 INFO 22252 --- [ main] o.a.c.c.C.[.[localhost].[/study-server] : Initializing Spring embedded WebApplicationContext
2021-08-25 23:02:04.189 INFO 22252 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1707 ms
2021-08-25 23:02:05.253 INFO 22252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path '/study-server'
2021-08-25 23:02:05.464 WARN 22252 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 23:02:05.469 WARN 22252 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 23:02:05.511 WARN 22252 --- [ main] d.s.r.o.OperationImplicitParameterReader : Unable to interpret the implicit parameter configuration with dataType: , dataTypeClass: class java.lang.Void
2021-08-25 23:02:05.539 INFO 22252 --- [ main] com.example.study.StudyApplication : Started StudyApplication in 3.699 seconds (JVM running for 4.103)
2021-08-25 23:02:05.541 INFO 22252 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-08-25 23:02:05.543 INFO 22252 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC