Skip to content

Linux 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.

  1. Capture credentials in an alias
Terminal window
$ connect alias add
Alias name: job1
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 'job1' saved. You can now use it as @job1/.. in commands.
  1. Verify connectivity and host key
Terminal window
$ connect ls @job1
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/ - 2024-11-27T13:39:18Z

Run the command again to ensure the stored host key is trusted without prompting.

  1. Prepare the connect move invocation
Terminal window
$ 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/
  1. Create the systemd service
Terminal window
$ systemctl --user edit --force --full job1.service
[Unit]
Description=SFTP job1 Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/apps/connect move --batch --no-color --parallel 3 /data/*.zip @job1/target/
  1. Create the timer unit
Terminal window
$ systemctl --user edit --force --full job1.timer
[Unit]
Description=Timer for job1 Service
[Timer]
OnCalendar=*:05:00
Persistent=false
[Install]
WantedBy=timers.target
  1. Enable and start
Terminal window
$ systemctl --user daemon-reload
$ systemctl --user enable job1.timer
Created 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
  1. Keep user services running after logout
Terminal window
$ sudo loginctl enable-linger apps