Sample bash script to start and stop Java application on Linux
Do you have trouble running your Java/Springboot applications on Linux? How do you start and stop your Java applications on a Linux server?
Executing a standalone Java application or Springboot executable jar in your local machine is simple and easy. You can use any of the Java IDE like Eclipse, Idea IntelliJ, NetBeans, etc. during development.
If you have developed a Java application that you need to execute on a Linux machine then you need a shell script to start your Java executable jar file. Without a proper shell script, it is a big pain to provide all required arguments in a command prompt every time to execute your Java application.
Here is a sample script that I prepared during my college days to execute my college project which I developed in Java. I have used this script for various other projects and I still found it helpful to execute my Springboot applications.
It is one of the traditional approaches not suitable for the applications which are container image and running as Docker container.
Let's first create a script to run your application.
Script 1: Script to execute your application - Run_MyApp_Script.sh
#!/bin/bash
#####################################
## Application service start script #
## Author: Prakash Singh Bhandari #
#####################################
export APP_CONFIG=[path for config directory]
APP_FILE=[application_file.jar]
appXmsSize=512m
appXmxSize=2048m
export VMARGS="-Xms$appXmsSize -Xmx$appXmsSize -Dlogging.config=$APP_CONFIG/logback-spring.xml"
## Start executable/Springboot application
$JAVA_HOME/bin/java $VMARGS -jar $APP_FILE --spring.config.location="file://$APP_CONFIG/application.properties" > /dev/null 2>&1 & echo "PID = $!" &
exit $?
You can also run your application by application Main class.
java -cp lib/*.jar com.myapp.MyAppMainClass
If your application is a spring boot executable jar file, then you can execute your application using the main class as given below
java -cp MyApplication.jar -Dloader.main=com.myapp.MyAppMainClass
org.springframework.boot.loader.PropertiesLauncher
--spring.config.location="file://application.properties"
You can modify the script based on your use case.
Now, we can create a service script that will ease our application start/stop.
Script 2: Service script - AppStart_Service.sh
This script is a generic script that contains three functions to start, stop and check the status of an application. I have used Unix "ps" command here. You can extend/alter this script based on your need.
The service script takes two arguments:
- Application file name and it should be same as application jar file name, the file name is used by the script to verify if the application is running or not.
- Script to start the application, which is given below as Script 2.
#!/bin/bash
#####################################
## Application service start script #
## Author: Prakash Singh Bhandari #
#####################################
export APP_NAME=$1
export APP_START_SCRIPT=$2
#function start the service
function app_start() {
PROC_DET="$(ps -eo pid,state,etime,time,%mem,%cpu,user,args | grep -v grep | grep java | grep $(whoami) | grep ${APP_NAME} )"
if [ -n "$PROC_DET" ]; then
while read procs; do
PROC_ID=$(echo $procs | awk '{print $1}')
echo "Application service is already running with PID = $PROC_ID "
echo " "
done <<< "$PROC_DET";
else
echo "Starting application - $APP_NAME"
${APP_START_SCRIPT}
fi;
sleep 5
#Check status
status;
}
function app_stop() {
PROC_DET="$(ps -eo pid,state,etime,time,%mem,%cpu,user,args | grep -v grep | grep java | grep $(whoami) | grep ${APP_NAME} )"
if [ -n "$PROC_DET" ]; then
while read procs; do
PROC_ID=$(echo $procs | awk '{print $1}')
echo "Application service with PID = $PROC_ID will be killed "
echo " "
kill $PROC_ID
echo " Process ID - $PROC_ID killed "
done <<< "$PROC_DET";
else
echo " $APP_NAME service is not running (already stopped)"
fi;
echo " "
echo " Checking status ..."
sleep 5
#Check status
status;
}
function app_status() {
echo " "
echo "--------------------------------------------------------------------------------"
printf '|%-25s|%-10s|\033[32m%-15s\033[37m|%-15s|%-15s|\n' "APP_NAME" "PROC_ID" "APP_STATUS" "APP_RUN_TIME" "APP_START_TIME"
echo "--------------------------------------------------------------------------------"
PROC_DET="$(ps -eo pid,state,etime,time,%mem,%cpu,user,args | grep -v grep | grep java | grep $(whoami) | grep ${APP_NAME} )"
if [ -n "$PROC_DET" ]; then
while read procs; do
PROC_ID=$(echo $procs | awk '{print $1}')
PROC_STATUS=$(echo $procs | awk '{print $2}')
PROC_RUN_TIME=$(echo $procs | awk '{print $3}')
PROC_START_TIME=$(ps eo bsdstart -p ${PROC_ID} h )
if [ "$PROC_STATUS" == "S" ]; then
PROC_STATUS="Running"
fi
printf '|%-25s|%-10s|\033[32m%-15s\033[37m|%-15s|%-15s|\n' "$APP_NAME" "$PROC_ID" "$PROC_STATUS" "$PROC_RUN_TIME" "$PROC_START_TIME"
done <<< "${PROC_DET}";
else
printf '|%-25s|%-10s|\033[32m%-15s\033[37m|%-15s|%-15s|\n' "$APP_NAME" "NA" "Not Running" "NA" "NA"
fi;
echo "--------------------------------------------------------------------------------"
echo " "
}
You can easily start and stop your application using these two scripts. First, you need to load the service script and then run simple commands to start, stop or check the current status of the application.
source AppStart_Service.sh MyApp /home/prakash/dev/bin/app_start/Run_MyApp_Script.sh
Once you source the script you can simply run the following commands:
- app_start - Start the application
- app_status - Display status of the application
- app_stop - Stop the application
As shown in the below screenshot:
The approach provided here is generic and can be used to run any executable ( not limited to Java applications) Let me know your approach to start and stop your Java applications on Linux machines also please comment if you have any better suggestions.
Comments
Post a Comment