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.
- First we need to test the connection
$ 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:282025/02/10 09:13:31 INFO Licensed for: UltimateEnter Private Key Passphrase:The key of 10.0.0.24:22 is unknown. Do you want to add this key to known_hosts (y/n): y2025/02/10 09:13:39 INFO Connection successful
- Then we need to store the key password in a file and rerun the connection test
$ 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_pass2025/02/10 09:15:49 INFO License valid to: 2026-04-13 18:06:282025/02/10 09:15:49 INFO Licensed for: Ultimate2025/02/10 09:15:50 INFO Connection successful$ cat /apps/send_passENC:QqKtxb2TeNoXXUIV7P64wJXy07tM33er9PPNcNozhz5lPg==
- Then we need to prepare the
connect sftp send
command
$ 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-color2025/02/10 09:36:44 INFO License valid to: 2026-04-13 18:06:282025/02/10 09:36:44 INFO Licensed for: Ultimate2025/02/10 09:36:47 INFO created directory name=target2025/02/10 09:36:47 INFO Transferring file=/data/file1.zip sftpSessionID=2 targetFolder=target2025/02/10 09:36:48 INFO Transferring file=/data/file3.zip sftpSessionID=1 targetFolder=target2025/02/10 09:36:48 INFO Transferring file=/data/file2.zip sftpSessionID=3 targetFolder=target2025/02/10 09:36:48 INFO File deleted file=/data/file1.zip sftpSessionID=22025/02/10 09:36:48 INFO File transferred successfully bytesTransferred=10485760 duration=912ms transferRate=10.96MB/s file=/data/file1.zip sftpSessionID=2 targetFolder=target2025/02/10 09:36:49 INFO File deleted file=/data/file2.zip sftpSessionID=32025/02/10 09:36:49 INFO File transferred successfully bytesTransferred=10485760 duration=876ms transferRate=11.41MB/s file=/data/file2.zip sftpSessionID=3 targetFolder=target2025/02/10 09:36:49 INFO File deleted file=/data/file3.zip sftpSessionID=12025/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
- Prepare sending service
$ systemctl --user edit --force --full send.service[Unit]Description=SFTP Send ServiceAfter=network-online.targetWants=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.
$ systemctl --user daemon-reload$ systemctl --user start send$ journalctl --user -eu sendFeb 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:28Feb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 INFO Licensed for: UltimateFeb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 INFO Sending files to sftp://[email protected]:targetFeb 10 10:11:37 arm connect[4036]: 2025/02/10 10:11:37 WARN No file to send
- Prepare SystemD Path unit
$ systemctl --user edit --force --full send.path[Unit]After=network.target
[Path]PathModified=/dataUnit=send.service
[Install]WantedBy=multi-user.target
- Enable SystemD Path unit
$ systemctl --user daemon-reload$ systemctl --user --now enable send.pathCreated symlink /apps/.config/systemd/user/default.target.wants/send.path → /apps/.config/systemd/user/send.path.
- Allow a user’s systemd services to run even after the user has logged out.
$ sudo loginctl enable-linger $(whoami)
- Check the job logs
$ journalctl --user -eu sendFeb 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:28Feb 10 10:54:35 arm connect[5750]: 2025/02/10 10:54:35 INFO Licensed for: UltimateFeb 10 10:54:35 arm connect[5750]: 2025/02/10 10:54:35 INFO Sending files to sftp://[email protected]:targetFeb 10 10:54:37 arm connect[5750]: 2025/02/10 10:54:37 INFO Transferring file=/data/test1.zip sftpSessionID=1 targetFolder=targetFeb 10 10:54:42 arm connect[5750]: 2025/02/10 10:54:42 INFO File deleted file=/data/test1.zip sftpSessionID=1Feb 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