CMP CMD > systemd path > watch dir then sync

· EOG, like EOF


Table of Contents

Perfect! Let’s make a ready-to-use Bash script that creates everything you need for a systemd.path + systemd.service workflow to automatically sync your Typora local blog folder to the SSHFS folder, ignoring temp files (.~*). We’ll use heredocs as requested.

Assumptions for paths (adjust if needed):

1LOCAL_DIR=~/blog-local
2REMOTE_DIR=~/blog-remote
3USER=$(whoami)
4SERVICE_NAME=blog-sync

Here’s the full script:

 1#!/bin/bash
 2
 3# === Configuration ===
 4LOCAL_DIR=~/blog-local
 5REMOTE_DIR=~/blog-remote
 6USER=$(whoami)
 7SERVICE_NAME=blog-sync
 8SYSTEMD_USER_DIR=~/.config/systemd/user
 9
10# Create systemd user directory if it doesn't exist
11mkdir -p "$SYSTEMD_USER_DIR"
12
13# === Create the systemd service file ===
14cat > "$SYSTEMD_USER_DIR/$SERVICE_NAME.service" <<-EOF
15[Unit]
16Description=Sync blog-local to blog-remote via rsync
17
18[Service]
19Type=oneshot
20ExecStart=/usr/bin/rsync -av --exclude='.~*' $LOCAL_DIR/ $REMOTE_DIR/
21EOF
22
23# === Create the systemd path file ===
24cat > "$SYSTEMD_USER_DIR/$SERVICE_NAME.path" <<-EOF
25[Unit]
26Description=Watch $LOCAL_DIR for changes and trigger blog sync
27
28[Path]
29PathModified=$LOCAL_DIR
30Unit=$SERVICE_NAME.service
31
32[Install]
33WantedBy=default.target
34EOF
35
36# === Reload systemd user daemon and enable/start the path ===
37systemctl --user daemon-reload
38systemctl --user enable --now $SERVICE_NAME.path
39
40echo "Systemd path & service created and enabled."
41echo "Monitoring $LOCAL_DIR and syncing changes to $REMOTE_DIR."

How it works #

  1. blog-sync.service → runs rsync once, ignoring Typora temp files (.~*).
  2. blog-sync.path → watches the local folder for modifications and triggers the service automatically.
  3. The script reloads systemd user units and starts the path, so it begins monitoring immediately.

Notes / Tips #

1ExecStart=/usr/bin/rsync -av --exclude='.~*' $LOCAL_DIR/ $REMOTE_DIR/ && /usr/bin/notify-send "Blog sync complete"
last updated: