springboot-Linux shell command to start,stop(kill) or restart springboot executable jar applications

1. The purpose of this post

Sometimes, we package the springboot applications as one single executable jar file and deploy it to the server. We start the jar as follows:

java -jar myapp-exec.jar

But when we want to stop it or restart it, we need to do follows:

ps -ef|grep java # find the pid of the app
kill -9 <thepid>

This work is a bit cumbersome,today, I would demo the linux shell commands to kill or restart springboot executable jar applications, you can use one-command to kill or restart the app.

2. Environments

  • SpringBoot 1.x or 2.x

3. The commands

3.1 Kill the SpringBoot App

If your springboot jar file name is myapp-exec.jar, kill it as this:

kill $(ps aux | grep 'myapp-exec.jar' | grep -v grep | awk '{print $2}')

Save the above script as stop.sh

3.2 Start the SpringBoot App in background

To start the app in background, you can use the nohup commmand:

nohup java -jar myapp-exec.jar > nohup.out &

Save the above script as start.sh

3.2 Restart the SpringBoot App

Now you can open an editor to create a file named restart.sh to do the restart job:

echo 'stop the app...'
./stop.sh
echo 'stopped'
sleep 2
echo 'start the app...'
./run.sh
echo 'started'

Save the above script as restart.sh