You can simply use "nohup java -jar yourappname.jar &" to run an application at backend , but how to automatically start this application as a service after server reboot ?
Step 1 : make your application a service
write a script as below and put to /etc/init.d/yourappname, make sure this script is readable and executable for the user who is going to run the 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 | |
### BEGIN INIT INFO | |
# Provides: yourappname | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start daemon at boot time | |
# Description: Enable service provided by daemon. | |
### END INIT INFO | |
SERVICE_NAME=jeh | |
PATH_TO_JAR=/opt/yourappname.jar | |
PID_PATH_NAME=/tmp/yourappname.pid | |
case $1 in | |
start) | |
echo "Starting $SERVICE_NAME ..." | |
if [ ! -f $PID_PATH_NAME ]; then | |
su - youruser -c "nohup java -jar $PATH_TO_JAR > /dev/null 2>&1 & echo $! > $PID_PATH_NAME" | |
echo "$SERVICE_NAME started ..." | |
else | |
echo "$SERVICE_NAME is already running ..." | |
fi | |
;; | |
stop) | |
if [ -f $PID_PATH_NAME ]; then | |
PID=$(cat $PID_PATH_NAME); | |
echo "$SERVICE_NAME stoping ..." | |
kill $PID; | |
echo "$SERVICE_NAME stopped ..." | |
rm $PID_PATH_NAME | |
else | |
echo "$SERVICE_NAME is not running ..." | |
fi | |
;; | |
restart) | |
if [ -f $PID_PATH_NAME ]; then | |
PID=$(cat $PID_PATH_NAME); | |
echo "$SERVICE_NAME stopping ..."; | |
kill $PID; | |
echo "$SERVICE_NAME stopped ..."; | |
rm $PID_PATH_NAME | |
echo "$SERVICE_NAME starting ..." | |
su - youruser -c "nohup java -jar $PATH_TO_JAR > /dev/null 2>&1 & echo $! > $PID_PATH_NAME" | |
echo "$SERVICE_NAME started ..." | |
else | |
echo "$SERVICE_NAME is not running ..." | |
fi | |
;; | |
esac |
After doing this you would be able to run below:
sudo service yourappname start
sudo service yourappname restart
sudo service yourappname stop
Step 2 : let system launch the application after reboot
$sudo update-rc.d yourappname defaults
Adding system startup for /etc/init.d/yourappname ...
/etc/rc0.d/KNNyourappname -> ../init.d/yourappname
/etc/rc1.d/KNNyourappname -> ../init.d/yourappname
/etc/rc6.d/KNNyourappname -> ../init.d/yourappname
/etc/rc2.d/KNNyourappname -> ../init.d/yourappname
/etc/rc3.d/KNNyourappname -> ../init.d/yourappname
/etc/rc4.d/KNNyourappname -> ../init.d/yourappname
/etc/rc5.d/KNNyourappname -> ../init.d/yourappname
Reboot your server , your application should be running.
Reference :
1. http://www.linfo.org/runlevel_def.html
2. https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples
3. https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-2-reference
4. https://www.ibm.com/developerworks/library/l-lpic1-v3-101-3/
No comments:
Post a Comment