tic2mqtt-c/run

82 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2021-08-05 22:09:41 +02:00
dev=/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A500DA2H-if00-port0
rundir=$(realpath $(dirname $0))
2021-09-10 10:45:00 +02:00
session="tic2mqtt"
cmd=""
quiet=""
2021-11-29 09:12:57 +01:00
cd $rundir
while [ $# -gt 0 ]; do
case "$1" in
start) cmd=$1;;
stop) cmd=$1;;
restart) cmd=$1;;
status) cmd=$1;;
2021-09-10 10:45:00 +02:00
log) cmd=$1;;
-q) quiet=$1;;
--quiet) quiet=$1;;
2021-09-10 10:45:00 +02:00
*) echo "usage: $0 [start|stop|restart|status|log] [-q|--quiet]"
exit 1
;;
esac
shift
done
2021-09-10 10:45:00 +02:00
is_running=$(tmux has -t $session 2> /dev/null && echo yes)
case "$cmd" in
start)
if [ -n "$is_running" ]; then
echo "tic2mqtt already running. aborting"
exit 1
fi
make -s
test -z $quiet && echo "starting tic2mqtt"
2021-09-10 10:45:00 +02:00
tmux new -s $session -d "./tic2mqtt -a -t $dev -h doctor -v"
test -z $quiet && tmux ls
;;
stop)
if [ ! -n "$is_running" ]; then
test -z $quiet && echo "no tic2mqtt running. nothing to stop"
exit 0
fi
test -z $quiet && echo "killing tic2mqtt"
2021-09-10 10:45:00 +02:00
tmux kill-session -t $session
;;
restart)
$0 $quiet stop
$0 $quiet start
;;
status)
if [ -n "$is_running" ]; then
2021-09-10 10:45:00 +02:00
tmux ls | grep $session
else
echo "no tic2mqtt running"
fi
;;
log)
if [ -n "$is_running" ]; then
2021-11-29 09:12:57 +01:00
tmp=$(mktemp)
tmux capture -b $session -p -S - >$tmp
if [ $(cat $tmp |wc -l) -gt 30 ]; then
head -10 $tmp
echo
echo "[...]"
echo
tail -20 $tmp
else
cat $tmp
fi
rm -f $tmp
else
echo "no tic2mqtt running"
fi
;;
*)
echo "usage: $0 [start|stop]"
;;
esac