Linux SystemD Timers
Moving files to SFTP with systemd timers
Section titled “Moving files to SFTP with systemd timers”Systemd timers give you predictable scheduling, dependency handling, and built-in logging. In this example every .zip found in /data/ is moved to sftp://monitor02@10.0.0.24/target/ with an SSH key that uses a passphrase. The transfer should happen at five minutes past each hour.
- Capture credentials in an alias
$ connect alias addAlias name: job1Endpoint (no folder, e.g. sftp://host or s3://bucket): sftp://monitor02@10.0.0.24Change scheme-specific settings? (y/N) ySFTP private key path (optional): /apps/keys/id_ed25519SFTP key passphrase (optional): ****Connection test: OKAlias 'job1' saved. You can now use it as @job1/.. in commands.- Verify connectivity and host key
$ connect ls @job1The 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): yName Size ModTimeincoming/ - 2024-11-27T13:39:18ZRun the command again to ensure the stored host key is trusted without prompting.
- Prepare the
connect moveinvocation
$ connect move --batch --no-color --parallel 3 \ \ /data/*.zip \ @job1/target/2024/11/27 15:45:51 INFO Moving files to sftp://monitor02@10.0.0.24/target/- Create the systemd service
$ systemctl --user edit --force --full job1.service[Unit]Description=SFTP job1 ServiceAfter=network-online.targetWants=network-online.target
[Service]ExecStart=/apps/connect move --batch --no-color --parallel 3 /data/*.zip @job1/target/- Create the timer unit
$ systemctl --user edit --force --full job1.timer[Unit]Description=Timer for job1 Service
[Timer]OnCalendar=*:05:00Persistent=false
[Install]WantedBy=timers.target- Enable and start
$ systemctl --user daemon-reload$ systemctl --user enable job1.timerCreated symlink /apps/.config/systemd/user/timers.target.wants/job1.timer → /apps/.config/systemd/user/job1.timer.$ systemctl --user start job1.timer$ systemctl --user status job1.timer● job1.timer - Timer for job1 Service Loaded: loaded (/apps/.config/systemd/user/job1.timer; enabled; preset: enabled) Active: active (waiting) since Thu 2024-11-28 11:49:59 UTC; 27s ago Trigger: Thu 2024-11-28 12:05:00 UTC; 14min left Triggers: ● job1.service- Keep user services running after logout
$ sudo loginctl enable-linger apps