Backend Notes JUN 12, 2024 • 02 MIN READ

BASH SCRIPT
TO AUTO RUN
JAVA APPLICATION

Abstract technical background

This script can be used to run command (status, start, stop) to manage your java application

Create file script.sh

script.sh BASH
#!/bin/bash

DIR_HOME=/home/pandz/repo/simulator-service
DIR_JAR=/home/pandz/repo/simulator-service/target/simulator-service-1.0.0-SNAPSHOT.jar
BRANCH=uat
SERVICE_NAME="simulator-service"
SONAR_URL=http://localhost:9000
SONAR_TOKEN=thisissonartoken

export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-17.0.8.0.7-2.el8.x86_64"

MS_PID=`ps fax|grep java|grep "${DIR_JAR}"|awk '{print $1}'`
export MS_PID;

start() {
  echo "Starting..."
  pid=${MS_PID}
  if [ -n "${pid}" ]; then {
    echo "${SERVICE_NAME} is already running (pid: ${pid})";
  } else {
    echo "Start run script ${SERVICE_NAME}...";
    cd $DIR_HOME
    git checkout ${BRANCH}
    git pull origin ${BRANCH}
    mvn clean install
    mvn sonar:sonar -Dsonar.projectKey=id.co.pandz:${SERVICE_NAME}-${BRANCH} -Dsonar.projectName=${SERVICE_NAME}-${BRANCH} -Dsonar.host.url=${SONAR_URL} -Dsonar.token=${SONAR_TOKEN}
    nohup java -jar ${DIR_JAR} -DLogback.configurationFile = ${DIR_HOME}/src/main/resources/logback-spring.xml &
  } fi;
}

stop() {
  pid=${MS_PID}
  if [ -n "${pid}" ]; then {
    kill -9 $pid
    sleep 10
  } else {
    echo "${SERVICE_NAME} is not running";
  } fi;
  pid=${MS_PID}
}

case $1 in
  start)
    start;
  ;;
  stop)
    stop;
  ;;
  status)
    pid=$MS_PID
    if [ "${pid}" ]; then {
      echo "${SERVICE_NAME} is running with pid: ${pid}";
    } else {
      echo "${SERVICE_NAME} is not running";
    } fi;
  ;;
esac

exit 0;

Add rule executable to file

chmod +x script.sh

How to use

To check status application ./script.sh status

To start application ./script.sh start

To stop application ./script.sh stop

READ BEYOND THE VOID

Technical blueprint background
Backend Notes

Service GRPC with Golang

GRPC using Golang...

Read Entry
Abstract digital network
Backend Notes

Housekeeping database Mysql on Centos server

Housekeeping your database today means fewer headaches tomorrow...

Read Entry
Circuit board macro
Backend Notes

Connect Webhook Gitlab

Just notes to myself...

Read Entry