switched to implementation with pipe due to possible race conditions

This commit is contained in:
root 2017-02-23 11:49:57 +00:00
parent 79098d7e62
commit 468576ecd0
2 changed files with 10 additions and 13 deletions

View File

@ -1,7 +1,6 @@
#!/bin/bash #!/bin/bash
COMMAND_FILE=/tmp/command COMMAND_FILE=/tmp/command
COMMAND_FILE_TMP=/tmp/command_tmp
lounge_light() { lounge_light() {
echo "not implemented" echo "not implemented"
@ -12,13 +11,12 @@ lounge_light_toggle() {
} }
cd "$(dirname $0)" cd "$(dirname $0)"
touch "$COMMAND_FILE" rm -f $COMMAND_FILE
chmod -f 666 "$COMMAND_FILE" mkfifo $COMMAND_FILE
while inotifywait -qq -e close_write "$COMMAND_FILE"; do chmod 666 $COMMAND_FILE
mv "$COMMAND_FILE" "$COMMAND_FILE_TMP" while true; do
sleep .1 # wait for possible further/parallel writes to end
while IFS='' read -r line || [[ -n "$line" ]]; do while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Text read from file: $line" echo "command received: $line"
case "$line" in case "$line" in
lounge_light_toggle) lounge_light_toggle)
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 echo "lounge_light lounge_light_toggle" >&2
;; ;;
esac esac
done < "$COMMAND_FILE_TMP" done < "$COMMAND_FILE"
rm "$COMMAND_FILE_TMP"
touch "$COMMAND_FILE"
chmod -f 666 "$COMMAND_FILE"
done done

View File

@ -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="/home/automation/chch-power/shell.sh",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAA...
COMMAND_FILE=/tmp/command COMMAND_FILE=/tmp/command
touch $COMMAND_FILE if [ ! -p $COMMAND_FILE ]; then
chmod -f 666 $COMMAND_FILE echo "ERROR; command watcher not listening:" >&2
exit 1
fi
status() { status() {
STATUS="$(cat /tmp/status)" STATUS="$(cat /tmp/status)"