Skip to content

Instantly share code, notes, and snippets.

@sangupta
Last active July 6, 2022 23:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sangupta/9d432f8368811533f99fec44b3ea9428 to your computer and use it in GitHub Desktop.
Save sangupta/9d432f8368811533f99fec44b3ea9428 to your computer and use it in GitHub Desktop.
A simple shell script to run a Java program from Linux shell in the background

This is a simple script to run a Java program from Linux shell in the background - so that the process does not terminates when you log out of the shell.

Usage is simple:

  • Start the process
$ ./java-app-run.sh start
  • Stop the process
$ ./java-app-run.sh stop

This makes sure that the process does not get launched twice. Creates a simple pid file in the path specified at APPPID.

#!/bin/sh
APPNAME="AEM Scoping Tool"
APPCODE=aemscope
APPBASE=/home/sangupta
APPPID=$APPBASE/runs/$APPCODE.pid
APPJAR=$APPBASE/aem-scope.jar
APPOPTS="-XX:-LoopUnswitching -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled"
case $1 in
start)
echo "Starting $APPNAME server ..."
if [ ! -f $APPPID ]; then
nohup java $APPOPTS -jar $APPJAR /home/admin/$APPCODE 2>> /dev/null >> /dev/null &
echo $! > $APPPID
echo "$APPNAME started!"
else
echo "$APPNAME is already running ..."
fi
;;
stop)
if [ -f $APPPID ]; then
PID=$(cat $APPPID);
echo "Stopping $APPNAME..."
kill $PID;
echo "$APPNAME stopped!"
rm $APPPID
else
echo "$APPNAME is not running ..."
fi
;;
*)
echo "Choose an option start/stop for the service"
;;
esac
@karmani1997
Copy link

I run on Linux Server but it not work properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment