[Summary, full note in Shaarli]
Comment créer un script d'init (/etc/init.d) pour une application Java (java -jar).
Structure du script (sh) :
- check_status : trouve le PID de l'appli ; renvoie 0 si elle ne tourne pas.
- start : si déjà lancée, "already started" et exit 1 ; sinon
java -jar /path/to/application.jar >> /path/to/logfile 2>&1 &
- stop : si arrêtée, "already stopped" et exit 1 ; sinon kill -9 $pid
- status : "Application is started" / "stopped".
- case "$1" in start|stop|status|restart|reload (= stop puis start), sinon usage : $0 {start|stop|restart|reload|status}.
EDIT : la recherche de PID de l'article ne marchait pas pour moi :
s=ps -C 'java -jar /path/to/application.jar' -o pid h
remplacée par :
s=$(pgrep --full 'une string unique qui identifie la command line pour lancer mon jar')
Attention : check_status fait "return $s", donc un PID passe par le code retour (limité à 0-255) ; à corriger si on réutilise le script.
How to run a jar the daemon way under linux
plus de détails sur comment créer un JAR
Comment créer un JAR à partir d'eclipse