Created
August 9, 2014 04:55
-
-
Save hmngwy/a712ef3357c39d32288c to your computer and use it in GitHub Desktop.
LanguageTool Server Init Script, run as a service. H/T: http://gustavostraube.wordpress.com/2009/11/05/writing-an-init-script-for-a-java-application/
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/sh | |
# | |
# init script for a Java application | |
# | |
# Check the application status | |
# | |
# This function checks if the application is running | |
check_status() { | |
# Running ps with some arguments to check if the PID exists | |
# -C : specifies the command name | |
# -o : determines how columns must be displayed | |
# h : hides the data header | |
s=`ps -C 'java -cp /path/to/languagetool/languagetool-server.jar org.languagetool.server.HTTPServer --public --port 80' -o pid h` | |
# If somethig was returned by the ps command, this function returns the PID | |
if [ $s ] ; then | |
return $s | |
fi | |
# In any another case, return 0 | |
return 0 | |
} | |
# Starts the application | |
start() { | |
# At first checks if the application is already started calling the check_status | |
# function | |
check_status | |
# $? is a special variable that hold the "exit status of the most recently executed | |
# foreground pipeline" | |
pid=$? | |
if [ $pid -ne 0 ] ; then | |
echo "The application is already started" | |
exit 1 | |
fi | |
# If the application isn't running, starts it | |
echo -n "Starting application: " | |
# Redirects default and error output to a log file | |
java -cp /path/to/languagetool/languagetool-server.jar org.languagetool.server.HTTPServer --public --port 80 >> /path/to/languagetool.log 2>&1 & | |
echo "OK" | |
} | |
# Stops the application | |
stop() { | |
# Like as the start function, checks the application status | |
check_status | |
pid=$? | |
if [ $pid -eq 0 ] ; then | |
echo "Application is already stopped" | |
exit 1 | |
fi | |
# Kills the application process | |
echo -n "Stopping application: " | |
kill -9 $pid & | |
echo "OK" | |
} | |
# Show the application status | |
status() { | |
# The check_status function, again... | |
check_status | |
# If the PID was returned means the application is running | |
if [ $? -ne 0 ] ; then | |
echo "Application is started" | |
else | |
echo "Application is stopped" | |
fi | |
} | |
# Main logic, a simple case to call functions | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
status) | |
status | |
;; | |
restart|reload) | |
stop | |
start | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|reload|status}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am using this in my CentOS 6.9 with a slight modification, but the main problem is that service languagetool stop . Doesn't works, because it can't find the pid of the service. The only way I found to stop the service is by editting the file and replacing kill -9 $pid & with kill -9 (actual pid number) & . Any leads how to resolve this ?