From 468576ecd0f5f8f66fc9578f6b0d404534078815 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 23 Feb 2017 11:49:57 +0000 Subject: [PATCH] switched to implementation with pipe due to possible race conditions --- command_watcher.sh | 17 ++++++----------- shell.sh | 6 ++++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/command_watcher.sh b/command_watcher.sh index 4baa8fb..77debaa 100755 --- a/command_watcher.sh +++ b/command_watcher.sh @@ -1,7 +1,6 @@ #!/bin/bash COMMAND_FILE=/tmp/command -COMMAND_FILE_TMP=/tmp/command_tmp lounge_light() { echo "not implemented" @@ -12,13 +11,12 @@ lounge_light_toggle() { } cd "$(dirname $0)" -touch "$COMMAND_FILE" -chmod -f 666 "$COMMAND_FILE" -while inotifywait -qq -e close_write "$COMMAND_FILE"; do - mv "$COMMAND_FILE" "$COMMAND_FILE_TMP" - sleep .1 # wait for possible further/parallel writes to end +rm -f $COMMAND_FILE +mkfifo $COMMAND_FILE +chmod 666 $COMMAND_FILE +while true; do while IFS='' read -r line || [[ -n "$line" ]]; do - echo "Text read from file: $line" + echo "command received: $line" case "$line" in lounge_light_toggle) lounge_light_toggle @@ -31,8 +29,5 @@ while inotifywait -qq -e close_write "$COMMAND_FILE"; do echo "lounge_light lounge_light_toggle" >&2 ;; esac - done < "$COMMAND_FILE_TMP" - rm "$COMMAND_FILE_TMP" - touch "$COMMAND_FILE" - chmod -f 666 "$COMMAND_FILE" + done < "$COMMAND_FILE" done diff --git a/shell.sh b/shell.sh index 05c8055..c507301 100755 --- a/shell.sh +++ b/shell.sh @@ -6,8 +6,10 @@ # command="/home/automation/chch-power/shell.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAA... COMMAND_FILE=/tmp/command -touch $COMMAND_FILE -chmod -f 666 $COMMAND_FILE +if [ ! -p $COMMAND_FILE ]; then + echo "ERROR; command watcher not listening:" >&2 + exit 1 +fi status() { STATUS="$(cat /tmp/status)"