From 30c0fe861bfeff859dad87c0ed4c45fff9c76e36 Mon Sep 17 00:00:00 2001 From: Fredrik Wahlberg Date: Thu, 23 Jan 2025 21:45:59 +0100 Subject: [PATCH] =?UTF-8?q?Script=20och=20service=20f=C3=B6r=20att=20?= =?UTF-8?q?=C3=B6vervaka=20och=20synka=20=C3=A4ndringar=20till=20filen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/org-todo-pwa.service | 10 ++++++++++ scripts/todo-watcher.sh | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 scripts/org-todo-pwa.service create mode 100755 scripts/todo-watcher.sh diff --git a/scripts/org-todo-pwa.service b/scripts/org-todo-pwa.service new file mode 100644 index 0000000..d508869 --- /dev/null +++ b/scripts/org-todo-pwa.service @@ -0,0 +1,10 @@ +[Unit] +Description=Synka Fredriks org-todo-pwa + +[Service] +ExecStart=/home/fredrik/bin/todo-watcher.sh +Restart=always +User=fredrik + +[Install] +WantedBy=multi-user.target diff --git a/scripts/todo-watcher.sh b/scripts/todo-watcher.sh new file mode 100755 index 0000000..8e5aced --- /dev/null +++ b/scripts/todo-watcher.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Set paths to the files +SOURCE_FILE="/srv/swarm/org-todo-pwa/data/tasks.org" +DEST_FILE="/home/fredrik/org-mode/pwa.org" + +# Watch for multiple events on the source file (e.g., modify, close_write, attrib) +inotifywait -m -e modify "$SOURCE_FILE" | while read path action file; do + # Compare the full path of the file being watched to the source file + if [ "$path$file" == "$SOURCE_FILE" ]; then + # Check if the file is empty (size 0) to avoid appending empty content + if [ -s "$SOURCE_FILE" ]; then + # Append the contents of the source file to the destination file + cat "$SOURCE_FILE" >> "$DEST_FILE" + + # Empty the source file + > "$SOURCE_FILE" + + echo "Appended contents of $SOURCE_FILE to $DEST_FILE and emptied the source file." + else + echo "$SOURCE_FILE is empty, skipping append." + fi + fi +done