Thursday, 26 May 2016

ubuntu : auto restart application as a service after server reboot


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.


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

Flag Counter