首页 > 系统相关 >Shell Scripts to start and Stop Spring Boot Applications.

Shell Scripts to start and Stop Spring Boot Applications.

时间:2024-12-18 17:25:04浏览次数:3  
标签:Shell Applications Spring script application sh file path start

In this post, We will learn about start and stop shell scripts to run Spring Boot applications.

Spring boot applications are easier to build and run. However, there is one small problem. Most of the developers still struggle when it comes to running, stopping, and starting the application in production servers. The traditional applications were deployed on a web container like tomcat which came with a startup.sh and shutdown.sh script. However, this is not available out of the box with spring boot. This article helps you create a simple start and stop script for your spring boot web application.

Start Script (startup.sh)

The startup script for Spring Boot is straightforward. Let's take our hello-world application.

#!/bin/bash
nohup java -jar /path/to/app/hello-world.jar > /path/to/log.txt 2>&1 &
echo $! > /path/to/app/pid.file

To break it down,

  • java -jar /path/to/app/hello-world.jar initializes the application.
  • > /path/to/log.txt redirects any anything written to STDOUT into the file /path/to/log.txt.
  • 2>&1 redirects all errors printed on STDERR to STDOUT. This way all the logs will go to STDOUT which is already redirected to /path/to/log.txt
  • & at the end makes the application run in the background.
  • echo $! will print the PID of the last command
  • The PID is then written to a file using > /path/to/app/pid.file
  • nohup lets the java process run in the background even after the user is logged out.

nohup may leave a nohup.out file in working directory when the STDOUT and STDERR is not redirected properly. If that's not what you want, You may want to use disown command to kick-start the script. A sample use case would be disown startup.sh &. But I dislike the way of calling a script with another command.

Stop Script (shutdown.sh)

Comparing to what we have written in the start script, the stop script is pretty simple.

#!/bin/bash
kill $(cat /path/to/app/pid.file)

The shutdown script makes use of the PID file created by the startup script. This allows us to locate and kill the exact process we spawned earlier. The script is pretty simple, and I believe this doesn't need any explanation.

stop and start (restart.sh)

I know that I'm overkilling the topic here. But for the sake of completeness, Here is your restart.sh.

#!/bin/bash
. /path/to/shutdown.sh
. /path/to/startup.sh

stop and start over SSH

You can stop and start your application straight from your local machines if you have a bash client using the following command. I personally use this method most of the time to restart my applications hosted on my servers.

sshpass -p password ssh userid@yourserver '/path/to/restart.sh'

Figure this one out yourself.

There are few points to note though.

  • Do not mix the name of the application jar files and paths. Always use canonical paths so its less confusing.
  • Do not mix the name of the PID files.
  • Your unix login may need necessary permissions to run few of the commands.
  • There may be parameters, active profiles and JVM arguments specific to your application. Make sure you configure them in the script.

Conclusion

To conclude, We learned how to write startup.sh, shutdown.sh, and restart.sh to manage the spring boot application process. If you feel like it, you can learn more about converting the application into a WAR file so that you can manage the application via the application server itself.

标签:Shell,Applications,Spring,script,application,sh,file,path,start
From: https://www.cnblogs.com/xzs603/p/18615433

相关文章

  • SpringCloud纲要
    介绍篇由于SpringCloud微服务内容纷繁。本篇主要作为一个简要的介绍,包含组件的种类,作用以及对应的博文链接。当前时间为2024年12月18日。写作时本人已有多年未涉足分布式SpringCloud服务,上次使用它实在17~18年,再回首进入官网时,许多组件发生了变迁,也增加了很多比较强悍的组件。我......
  • 基于Spring Boot + SSM(Spring + Spring MVC + MyBatis)+Vue+MySQL实现一个简单的用户管
    后端代码(SpringBoot+SSM部分)1.创建SpringBoot项目使用SpringInitializr(可以通过IDEA等IDE自带的创建SpringBoot项目功能,或者访问Spring官网的Initializr页面)创建一个基础的SpringBoot项目,添加相关依赖,比如Web(用于构建Web应用)、MyBatis、MyBatisSpringBootStarter、My......
  • Spring源码分析之ConfigurationClassPostProcessor
    前言 在通过Spring源码分析之容器Refresh()方法_spring源码中refresh()方法-CSDN博客我们知到其中有一个步骤就是说会将满足条件的类注册为BeanDefinition然后放入到Spring容器中,这个主要就是存在于invokeBeanFactoryPostProcessors这个方法中进行的这个就是说具体是怎么......
  • 简单的基于Spring Cloud和Vue的示例项目结构及部分关键代码
    后端(SpringCloud部分)1.创建SpringCloud项目(以SpringCloudGateway和SpringCloudEureka为例)首先,使用SpringInitializr创建一个基础的SpringBoot项目,并添加相关的SpringCloud依赖,比如:spring-cloud-starter-gateway:用于实现API网关功能。spring-cloud-starter-netflix-......
  • Java程序员如何高效学习Spring Cloud Alibaba?
    SpringCloudAlibaba有多香?大家都知道SpringCloudAlibaba是阿里巴巴提供的微服务开发一站式解决方案,是阿里巴巴开源中间件与SpringCloud体系的融合。依托SpringCloudAlibaba,您只需要添加一些注解和少量配置,就可以将SpringCloud应用接入阿里微服务解决方案,通过阿......
  • Spring Cloud常用组件及其配置
    一、Eureka(服务注册与发现)EurekaServer配置application.yml配置示例:server:port:8761eureka:instance:hostname:localhostclient:register-with-eureka:falsefetch-registry:false解释:server.port:指定EurekaServer运行的端口,这里是8761......
  • Spring Cloud 开发环境搭建
    一、环境准备Java环境SpringCloud是基于Java开发的框架,所以需要先安装Java开发工具包(JDK)。确保你的系统安装了JDK8或更高版本。你可以从Oracle官方网站(https://www.oracle.com/java/technologies/javase-downloads.html)或OpenJDK官方网站(https://openjdk.java.net/install......
  • springboot毕设商会会员管理系统程序+论文+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容一、研究背景随着市场经济的不断发展,商会在商业领域的重要性日益凸显。商会会员数量逐渐增多,会员之间的交流、合作以及商会对会员的管理变得越发复杂。传统的......
  • springboot毕设收纳师管理系统论文+程序+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容一、研究背景在现代社会,随着人们生活节奏的加快和生活品质要求的提高,收纳需求日益增长。然而,忙碌的生活使得很多人无暇顾及个人和家庭的收纳事务,收纳师这一新......
  • springboot毕设图像数字知识产权保护系统论文+程序+部署
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容一、研究背景在当今数字化时代,图像作为一种重要的信息载体,其创作、传播和使用日益频繁。随着数字技术的飞速发展,图像的复制、篡改和盗用变得更加容易,这给图像......