Operations 7 min read

Automate Java Service Deployment with One-Click CI/CD Shell Scripts

This guide shows how to replace tedious manual jar deployment with a simple one‑click CI/CD solution, providing stop and start shell scripts, Jenkins integration, and deployment automation to speed up testing and production releases.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Java Service Deployment with One-Click CI/CD Shell Scripts

Background (Taking It Easy)

In a small company, repeatedly performing manual deployments becomes exhausting; uploading a 200 MB JAR over a slow FTP connection, locating the process with jps, killing it, and restarting the service causes physical and mental discomfort, contradicting the personal motto: "Laziness is the first driver of human progress."

Self‑Rescue Path

The goal is simple: obtain a one‑click CI/CD tool to keep laziness productive. To achieve this, the following work was done.

Stop Service Script (Optimized)

The recommended stop script (with a 5‑second sleep added) only requires changing the APP_MAINCLASS variable to your JAR name; everything else remains untouched.

#!/bin/bash
# Main class
APP_MAINCLASS="XXX-1.0.0.jar"
# Process ID
psid=0
# Attempt count
num=0
getpid() {
   javaps=`jps -l | grep $APP_MAINCLASS`
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}
stop() {
   getpid
   num=`expr $num + 1`
   if [ $psid -ne 0 ]; then
      if [ "$num" -le 3 ]; then
         echo "attempt to kill... num:$num"
         kill $psid
         sleep 5
      else
         echo "force kill..."
         kill -9 $psid
      fi
      if [ $? -eq 0 ]; then
         echo "Shutdown success..."
      else
         echo "Shutdown failed..."
      fi
      getpid
      if [ $psid -ne 0 ]; then
         echo "getpid... num:$psid"
         stop
      fi
   else
      echo "App is not running"
   fi
}
stop

Start Service Script (2 Lines)

Replace XXX-1.0.0.jar with your own JAR name and save the content as start.sh. JVM parameters can be adjusted as needed.

basepath=$(cd `dirname $0`; pwd)
nohup java -server -Xmx2g -Xms2g -Xmn1024m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:-UseAdaptiveSizePolicy -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails -Xloggc:logs/gc.log -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:HeapDumpPath=logs/dump.hprof -XX:ParallelGCThreads=4 -jar $basepath/XXX-1.0.0.jar >nohup.log 2>&1 &

Reuse the existing Jenkins job and place the scripts in the Post Steps.

The nine‑line deployment script performs four main actions:

Back up the currently running JAR (enabling quick rollback).

Copy the newly built package from Jenkins to the target server.

Execute the stop‑service script.

Execute the start‑service script.

Deployment script example:

ssh -Tq $IP << EOF
source /etc/profile
# Enter deployment directory
cd /data/app/test
DATE=`date +%Y-%m-%d_%H-%M-%S`
# Remove old backup JARs
rm -rf /data/app/test/xxx-1.0.0.jar.bak*
# Backup current JAR
mv /data/app/test/xxx-1.0.0.jar /data/app/test/xxx-1.0.0.jar.bak$DATE
# Pull latest JAR from Jenkins
scp root@$jenkisIP:/data/jenkins/workspace/test/target/XXX-1.0.0.jar /data/app/test
# Stop the application
sh /data/app/test/stop.sh
# Start the application
sh /data/app/test/start.sh
exit
EOF

Note:

$IP is the deployment server IP; $jenkisIP is the Jenkins server IP. Ensure password‑less SSH between them before deployment.

/data/app/test is the directory where the JAR is stored.

stop.sh refers to the stop script described earlier.

start.sh refers to the start script described earlier.

Conclusion

If you are tired of local packaging, slow JAR uploads, and manual stop/start procedures, try this automated deployment script. A small investment yields a large return.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationDeploymentDevOpsJenkinsshell scriptCI/CD
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.