Hier wird gezeigt, wie man mit Hilfe des Process Control Systems "supervisor" Programme als Daemon laufen lassen kann.
Dabei werden die Prozesse ggf. neu gestartet und auch ist es möglich, die eingebunden Dienste über eine WEB-Seite zu managen.
http://supervisord.org/
Forumthread iOS-Widget:
https://forum.fhem.de/index.php/topic,45328.msg493181.html#msg493181
Als Beispiel die Installation auf Debian (Auch für Raspberry).
Zuerst die Suite installieren
apt-get install supervisor
Dann eine Java Umgebung installieren (falls nicht schon vorhanden)
apt-get install default-jre-headless
Edit /etc/supervisor/supervisord.conf
Wichtig: den Part [inet_http_server] für das WEB-Interface aktivieren. Dabei einen freien Port nehmen. Hier z.B. 9001
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[inet_http_server]
port=*:9001
username=admin
password=admin
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
logfile_maxbytes=20MB ; maximum size of logfile before rotation
logfile_backups=1
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
Die Config-Files liegen unter /etc/supervisor/conf.d
Erzeugen von /etc/supervisor/conf.d/fhem_sync_client.conf
[program:FhemSyncClientHook]
command=/opt/fhem/fhem_sync_client.sh
autostart=true
autorestart=true
startretries=3
stopwaitsec=10
#stopasgroup=true
stderr_logfile=/var/log/supervisor/fhem_sync_client-err.log
stdout_logfile=/var/log/supervisor/fhem_sync_client-std.log
user=root
http://paste.debian.net/843311/
Erstellen des Bash-Scripts für den Fhem-Sync-Client.
File: /opt/fhem/fhem_sync_client.sh
Nach dem Editieren nicht vergessen!
chmod +x /opt/fhem/fhem_sync_client.sh
#!/bin/bash
#################################################
# Fhem_Sync_Client bootscript by armin@pipp.at
# version:0.9 - 9/2016
#
#################################################
# Kill the java client process if shell script terminates
trap "kill -- -$$" EXIT
# ---- change this ----
DIR="/opt/fhem/sync_client"
URL="https://tasior.info:42333"
JAR="FHEM_Sync_Client.jar"
FHEMURL="http://localhost:8083"
JAVAOPT="-jar $DIR/$JAR -v -url=$FHEMURL"
# ---- internal ----
test -d $DIR || mkdir -p $DIR
test -d $DIR/tmp || mkdir -p $DIR/tmp
test -f $DIR/chk.txt || echo x >$DIR/chk.txt
WGETERR="0"
DIFF="0"
function killjavaclient(){
echo "kill:$JAR"
ps axf | grep "$JAR" | grep -v grep | awk '{system("kill -9 " $1)}'
sleep 2
}
function updatefunc() {
echo "Download Client"
echo "wget -P $DIR/tmp $URL/$JAR"
# remove old Jar
test -f $DIR/tmp/$JAR && rm $DIR/tmp/$JAR
# get jar with wget
wget -P $DIR/tmp $URL/$JAR
WGETERR=$?
}
###### start main ######
while true; do
HOUR=`date +%H`
DAY=`date +%d`
CHKTMP=`cat $DIR/chk.txt`
CHK="$DAY.$HOUR"
echo "CHK :$CHK"
echo "CHKTMP:$CHKTMP"
if ! [ "$CHK" == "$CHKTMP" ]; then
# run only once a hour to prevent much downloads
updatefunc
# check wget success
if [ $WGETERR -ne 0 ]; then
echo "Wget Error:$WGETERR"
else
# check if files diffs
cmp --silent $DIR/$JAR $DIR/tmp/$JAR || DIFF="1"
fi
echo $CHK>$DIR/chk.txt
echo "DIFF:$DIFF"
killjavaclient
if [ "$DIFF" == "1" ]; then
echo "Aktiviere neue Datei:$JAR"
cp $DIR/tmp/$JAR $DIR
# $JAR.master exists copy master over downloaded once - only for testing
test -f $DIR/$JAR.master && cp $DIR/$JAR.master $DIR/$JAR
fi
fi
# start Java client
echo "Starte $JAR"
java $JAVAOPT
echo "Java-client beendet oder gestorbern. Egal, Hauptsache es gibt eine neue Version."
sleep 10
done
Funktionen des Scripts fhem_sync_client.sh:
Das Tool startet den Java Sync Client für das iOS Widget und lädt ggf. eine neue Version vom Server und installiert und startet diese automatisch.
Weiters wird das Laden des Clients nur pro Stunde 1x gemacht um bei etwaigen Fehlern nicht einen hohen Datentransfer durch die ständigen Download zu generieren.
Es wird angenommen, dass sich FHEM am selben Server befindet. Diese Einstellungen können in den Variablen am Anfang des Scripts einfach geändert werden.
Bedienung des supervisord:
Im Browser das WEB-Interface unter Port:9001 aufrufen:
http://<fhem-ip>:9001
Username und Passwort = admin
Nun kann der Dienst dort verwaltet werden. (Start/Stop/Tail -f)
Ein Screenshot ist im Anhang.
Auf der Console werden die konfigurierten Prozesse mit dem supervisorctl kontrolliert.
Hilfe:
supervisorctl help
Nach dem Ändern in conf.d
supervisorctl reread
supervisorctl updat
Status:
supervisorctl status
FhemSyncClientHook RUNNING pid 17277, uptime 1:38:00
Starten und Stoppen:
supervisorctl stop FhemSyncClientHook
FhemSyncClientHook: stopped
supervisorctl start FhemSyncClientHook
FhemSyncClientHook: started
Neustart über HTTP Request:
curl --silent 'http://admin:admin@<supervisord-IP>:9001/index.html?processname=FhemSyncClientHook&action=restart'
curl --silent 'http://admin:admin@fhem3.local:9001/index.html?processname=FhemSyncClientHook&action=restart'