docker stop command send SIGTERM to pid1 inside the container to let a chance to stop gracefully (https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/)
Unfortunatly some software ignore SIGTERM and need to be stopped by their own command (hello opendj)
To do so, in the Dockerfile, in the CMD, I use a script like this :
/thecommand/tostart/thesoftware/inbackground
trap "/thecommand/tostop/thesoftware" SIGTERM
while true; do sleep 1; done
so my script is run with PID1, docker stop sends SIGTERM, my script catch (trap) the signal, and run the command to stop gracefully
à mettre dans son bashrc
showOriginal() {
packageName=$(dpkg -S $1|cut -d':' -f1)
tmpdir="/tmp/showOriginal-$packageName-$(date +%s)"
mkdir $tmpdir
cd $tmpir
apt-get download $packageName > /dev/null
dpkg -x *.deb $tmpdir
cat ${tmpdir}$1
cd - >/dev/null
rm -rf $tmpdir
}
[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.
Je me garde ça au chaud, ça peut servir
Script pour backup toutes ses bases dans des fichiers séparés
Un wiki complet à lire sur quelques outils en bash
xargs sed cut awk grep find while for