Skip to content

Linux SystemD Path

A systemd path unit can watch a directory and trigger Connect whenever something changes. Here we move any new .zip in /data/ straight to the remote folder sftp://monitor02@10.0.0.24/target/ using an SSH key with a passphrase.

  1. Create an alias for the endpoint
Terminal window
$ connect alias add
Alias name: send
Endpoint (no folder, e.g. sftp://host or s3://bucket): sftp://monitor02@10.0.0.24
Change scheme-specific settings? (y/N) y
SFTP private key path (optional): /apps/keys/id_ed25519
SFTP key passphrase (optional): ****
Connection test: OK
Alias 'send' saved. You can now use it as @send/.. in commands.
  1. Accept the host key and test the alias
Terminal window
$ connect ls @send
The key (SHA256:v/goXq57T++lsDmeYduLCRJEUzEqf9u9OybWiHt3VRc) of 10.0.0.24:22 is unknown. Do you want to add this key to known_hosts (y/n): y
Name Size ModTime
incoming/ - 2025-02-10T09:13:39Z
  1. Run the intended move command once
Terminal window
$ connect move --batch --no-color --parallel 3 \
/data/*.zip \
@send/target/
2025/02/10 09:36:44 INFO Moving files to sftp://monitor02@10.0.0.24/target/
2025/02/10 09:36:49 INFO Move finished no more matches
  1. Create the service unit that performs the move
Terminal window
$ systemctl --user edit --force --full send.service
[Unit]
Description=SFTP Send Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/apps/connect move --batch --no-color --parallel 3 /data/*.zip @send/target/

Reload systemd and test the service manually:

Terminal window
$ systemctl --user daemon-reload
$ systemctl --user start send.service
$ journalctl --user -eu send.service | tail
Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 WARN No matching files for /data/*.zip
  1. Create the path unit that watches /data
Terminal window
$ systemctl --user edit --force --full send.path
[Unit]
Description=Watch /data for new files
[Path]
PathModified=/data
Unit=send.service
[Install]
WantedBy=default.target
  1. Enable both units
Terminal window
$ systemctl --user daemon-reload
$ systemctl --user enable --now send.path
Created symlink /apps/.config/systemd/user/default.target.wants/send.path /apps/.config/systemd/user/send.path.
  1. Keep the user service alive after logout
Terminal window
$ sudo loginctl enable-linger $(whoami)
  1. Review triggered runs
Terminal window
$ journalctl --user -eu send.service --since "1 hour ago"