Skip to content

Linux SystemD Path

Sending files to a remote SFTP immediately

Section titled “Sending files to a remote SFTP immediately”

In modern Linux systems there’s SystemD path unit. It allows you to monitor specific directory and trigger connect tool to automatically transmit data.

Let’s say we’d like to send all .zip files from /data folder to target folder of SFTP server 10.0.0.24 using monitor02 user and id_ed25519 key that is password protected.

Moreover we’d like those files to be send automatically as soon as they appear in the /data folder.

  1. First we need to test the connection
Terminal window
$ connect test sftp -h 10.0.0.24 -l monitor02 -i /apps/keys/id_ed25519 -k -
2025/02/10 09:13:31 INFO License valid to: 2026-04-13 18:06:28
2025/02/10 09:13:31 INFO Licensed for: Ultimate
Enter Private Key Passphrase:
2025/02/10 09:13:33 INFO Testing connection to sftp://[email protected]
The key of 10.0.0.24:22 is unknown. Do you want to add this key to known_hosts (y/n): y
2025/02/10 09:13:39 INFO Connection successful

  1. Then we need to store the key password in a file and rerun the connection test
Terminal window
$ echo "pass" > /apps/send_pass
$ connect test sftp -h 10.0.0.24 -l monitor02 -i /apps/keys/id_ed25519 --identity-passphrase-file /apps/send_pass
2025/02/10 09:15:49 INFO License valid to: 2026-04-13 18:06:28
2025/02/10 09:15:49 INFO Licensed for: Ultimate
2025/02/10 09:15:49 INFO Testing connection to sftp://[email protected]
2025/02/10 09:15:50 INFO Connection successful
$ cat /apps/send_pass
ENC:QqKtxb2TeNoXXUIV7P64wJXy07tM33er9PPNcNozhz5lPg==

  1. Then we need to prepare the connect sftp send command
Terminal window
$ connect send sftp \
-h 10.0.0.24 \
-l monitor02 \
-i /apps/keys/id_ed25519 \
--identity-passphrase-file /apps/send_pass \
-s /data \
--file-mask "*.zip" \
-d target \
--delete \
--parallel 3 \
--batch \
--no-color
2025/02/10 09:36:44 INFO License valid to: 2026-04-13 18:06:28
2025/02/10 09:36:44 INFO Licensed for: Ultimate
2025/02/10 09:36:44 INFO Sending files to sftp://[email protected]:target
2025/02/10 09:36:47 INFO created directory name=target
2025/02/10 09:36:47 INFO Transferring file=/data/file1.zip sftpSessionID=2 targetFolder=target
2025/02/10 09:36:48 INFO Transferring file=/data/file3.zip sftpSessionID=1 targetFolder=target
2025/02/10 09:36:48 INFO Transferring file=/data/file2.zip sftpSessionID=3 targetFolder=target
2025/02/10 09:36:48 INFO File deleted file=/data/file1.zip sftpSessionID=2
2025/02/10 09:36:48 INFO File transferred successfully bytesTransferred=10485760 duration=912ms transferRate=10.96MB/s file=/data/file1.zip sftpSessionID=2 targetFolder=target
2025/02/10 09:36:49 INFO File deleted file=/data/file2.zip sftpSessionID=3
2025/02/10 09:36:49 INFO File transferred successfully bytesTransferred=10485760 duration=876ms transferRate=11.41MB/s file=/data/file2.zip sftpSessionID=3 targetFolder=target
2025/02/10 09:36:49 INFO File deleted file=/data/file3.zip sftpSessionID=1
2025/02/10 09:36:49 INFO File transferred successfully bytesTransferred=10485760 duration=882ms transferRate=11.33MB/s file=/data/file3.zip sftpSessionID=1 targetFolder=target
2025/02/10 09:36:49 INFO Disconnected sftpSessionID=2 target=sftp://[email protected]:
2025/02/10 09:36:49 INFO Disconnected sftpSessionID=3 target=sftp://[email protected]:
2025/02/10 09:36:49 INFO Disconnected sftpSessionID=1 target=sftp://[email protected]:

  1. Prepare sending service
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 send sftp -h 10.0.0.24 -l monitor02 --password-file /apps/send_pass -s /data --file-mask "*.zip" -d target --delete --parallel 3 --batch --no-color

Reload systemd, start the service one time and check the logs.

Terminal window
$ systemctl --user daemon-reload
$ systemctl --user start send
$ journalctl --user -eu send
Feb 10 10:11:37 arm systemd[1270]: Started send.service - SFTP Send Service.
Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 INFO License valid to: 2026-04-13 18:06:28
Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 INFO Licensed for: Ultimate
Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 INFO Sending files to sftp://[email protected]:target
Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 WARN No file to send
  1. Prepare SystemD Path unit
Terminal window
$ systemctl --user edit --force --full send.path
[Unit]
After=network.target
[Path]
PathModified=/data
Unit=send.service
[Install]
WantedBy=multi-user.target

  1. Enable SystemD Path unit
Terminal window
$ systemctl --user daemon-reload
$ systemctl --user --now enable send.path
Created symlink /apps/.config/systemd/user/default.target.wants/send.path /apps/.config/systemd/user/send.path.

  1. Allow a user’s systemd services to run even after the user has logged out.
Terminal window
$ sudo loginctl enable-linger $(whoami)

  1. Check the job logs
Terminal window
$ journalctl --user -eu send
Feb 10 10:54:34 arm systemd[1270]: Started send.service - SFTP Send Service.
Feb 10 10:54:35 arm connect[5750]: 2025/02/10 10:54:35 INFO License valid to: 2026-04-13 18:06:28
Feb 10 10:54:35 arm connect[5750]: 2025/02/10 10:54:35 INFO Licensed for: Ultimate
Feb 10 10:54:35 arm connect[5750]: 2025/02/10 10:54:35 INFO Sending files to sftp://[email protected]:target
Feb 10 10:54:37 arm connect[5750]: 2025/02/10 10:54:37 INFO Transferring file=/data/test1.zip sftpSessionID=1 targetFolder=target
Feb 10 10:54:42 arm connect[5750]: 2025/02/10 10:54:42 INFO File deleted file=/data/test1.zip sftpSessionID=1
Feb 10 10:54:42 arm connect[5750]: 2025/02/10 10:54:42 INFO File transferred successfully bytesTransferred=104857600 duration=5s484ms transferRate=18.23MB/s file=/data/test1.zip sftpSessionID=1 targetFolder=target