Last active
December 20, 2023 00:38
-
-
Save MathOliveira/f7c0a8a7be7eb511ca0ff5a1aceed995 to your computer and use it in GitHub Desktop.
Creating your shell script to start/stop a Java application on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With this script you can turn your jar file into a service on linux. | |
Change the MATH constant to the name of your application and make sure you are correctly specifying | |
the locationsof the files used in the variables. | |
For the correct operation of the script install it in the directory init.d and release its access as below: | |
chmod 755 *your_script* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: MATH | |
# Required-Start: $java $pgrep | |
# Required-Stop: $java $pgrep | |
# Short-Description: Start and stop MATH service. | |
# Description: MATH it's a java appplication example | |
# Date-Creation: 2018-09-26 | |
# Date-Last-Modification: 2018-09-26 | |
# Author: Matheus Sant"Anna de Oliveira | |
### END INIT INFO | |
# Variables | |
APPDIR=/opt/MATH | |
PGREP=/usr/bin/pgrep | |
JAVA=/usr/bin/java | |
MATHJARFILE=/MATH.jar | |
ZERO=0 | |
OKMSG=OK | |
MATHPORT=3131 | |
#Colors | |
RED='\033[1;31m' | |
GREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
BLUE='\033[1;34m' | |
NC='\033[0m' #No Color | |
# Start the MATH | |
start() { | |
echo "" | |
echo "Starting MATH..." | |
#Verify if the service is running | |
$PGREP -f MATH > /dev/null | |
VERIFIER=$? | |
if [ $ZERO -eq $VERIFIER ] | |
then | |
echo -e "The service is ${YELLOW}already${NC} running" | |
else | |
#Run the jar file MATH service | |
$JAVA -jar "-Dfile.encoding=UTF-8" $APPDIR$MATHJARFILE q > /dev/null 2>&1 & | |
#sleep time before the service verification | |
sleep 3 | |
#Verify if the service is running | |
$PGREP -f MATH > /dev/null | |
VERIFIER=$? | |
if [ $ZERO -eq $VERIFIER ] | |
then | |
echo -e "Service was ${GREEN}successfully${NC} started" | |
else | |
echo -e "${RED}Failed${NC} to start service" | |
fi | |
fi | |
echo | |
} | |
# Stop the MATH | |
stop() { | |
echo "" | |
echo "Stopping MATH..." | |
#Verify if the service is running | |
$PGREP -f MATH > /dev/null | |
VERIFIER=$? | |
if [ $ZERO -eq $VERIFIER ] | |
then | |
#Kill the pid of java with the service name | |
kill -9 $($PGREP -f MATH) | |
#Sleep time before the service verification | |
sleep 3 | |
#Verify if the service is running | |
$PGREP -f MATH > /dev/null | |
VERIFIER=$? | |
if [ $ZERO -eq $VERIFIER ] | |
then | |
echo -e "${RED}Failed${NC} to stop service" | |
else | |
echo -e "Service was ${GREEN}successfully${NC} stopped" | |
fi | |
else | |
echo -e "The service is ${YELLOW}already${NC} stopped" | |
fi | |
echo | |
} | |
# Verify the status of MATH | |
status() { | |
echo "" | |
echo "Checking status of MATH..." | |
img | |
#Verify if the service is running | |
$PGREP -f MATH > /dev/null | |
VERIFIER=$? | |
if [ $ZERO -eq $VERIFIER ] | |
then | |
echo -e "Service is ${GREEN}running${NC}" | |
else | |
echo -e "Service is ${RED}stopped${NC}" | |
fi | |
echo | |
} | |
# Main logic from script | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
restart|reload) | |
stop | |
start | |
;; | |
*) | |
echo "${RED}Invalid arguments${NC}" | |
echo " Usages: $0 ${BLUE}{ start | stop | status | restart | reload }${NC}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment