Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace cron jobs with systemd timers on infra systems #590

Closed
jbergstroem opened this issue Jan 8, 2017 · 5 comments
Closed

replace cron jobs with systemd timers on infra systems #590

jbergstroem opened this issue Jan 8, 2017 · 5 comments
Labels

Comments

@jbergstroem
Copy link
Member

Systems running systemd can use the built in timers functionality which is intended to replace cron. The benefits (for us) using these are:

  • scripts are likely more reproducible over multiple machines (read: git check in)
  • easier to track:
    # systemctl list-timers --all
    NEXT                         LEFT          LAST                         PASSED    UNIT                         ACTIVA
    Sun 2017-01-08 18:45:00 UTC  4min 23s left Sun 2017-01-08 18:30:25 UTC  10min ago rsyncmirror.timer            rsyncm
    Sun 2017-01-08 21:05:36 UTC  2h 24min left Sat 2017-01-07 21:05:37 UTC  21h ago   systemd-tmpfiles-clean.timer system
    Sun 2017-01-08 22:48:09 UTC  4h 7min left  Sun 2017-01-08 08:50:37 UTC  9h ago    apt-daily.timer              apt-da
    n/a                          n/a           n/a                          n/a       ureadahead-stop.timer        ureada
    
    4 timers listed.
    

I'd like to move things we do in cron today, such as rsync:ing backups, benchmark and so on to this. Thoughts?

@jbergstroem
Copy link
Member Author

Syntax btw (for above rsync script):

# tail -n +1 /etc/systemd/system/rsyncmirror.*
==> /etc/systemd/system/rsyncmirror.service <==
[Unit]
Description=runs a rsync mirror against nodejs main server

[Service]
Type=oneshot
ExecStart=/usr/bin/rsync -e "ssh" -a -q --exclude "doc" --exclude "docs" --exclude "metrics" --exclude "tools" --exclude ".*" --exclude "rsync-old-nodejs.org.sh" --exclude "www" --delete root@host:/home/dist/ /home/dist/

==> /etc/systemd/system/rsyncmirror.timer <==
[Unit]
Description=run the rsync mirror job every 15 minutes

[Timer]
OnCalendar=*:0/15

@gibfahn
Copy link
Member

gibfahn commented Jan 9, 2017

Makes sense to me if it means we can reuse scripts.

@jbergstroem
Copy link
Member Author

@gibfahn said:
Makes sense to me if it means we can reuse scripts.

I guess the same applies for a checked in cron job (but os-specific things could be different).

I just really liked the tools to control timed scripts; getting full log history and confirmation that it ran successfully. It takes a bit more polish to get cron jobs to that shape.

@joaocgreis
Copy link
Member

Syntax as listed by @jbergstroem above does not make the timer active on startup, so a reboot disables the timer. An [Install] section is required, and the timer must be explicitly enabled with systemctl enable.

Updated syntax for reference:

# tail -n +1 /etc/systemd/system/rsyncmirror.*
==> /etc/systemd/system/rsyncmirror.service <==
[Unit]
Description=runs a rsync mirror against nodejs main server

[Service]
Type=oneshot
ExecStart=/usr/bin/rsync -e "ssh" -a -q --exclude "www" --exclude "doc" --exclude "metrics" --exclude "tools" --exclude ".*" --exclude "rsync-old-nodejs.org.sh" --delete root@direct.nodejs.org:/home/dist/ /home/dist/
ExecStartPost=/usr/bin/rsync -e "ssh" -a -q --delete root@direct.nodejs.org:/home/www/ /home/www/

==> /etc/systemd/system/rsyncmirror.timer <==
[Unit]
Description=run the rsync mirror job every 15 minutes

[Timer]
OnBootSec=1min
OnUnitActiveSec=15min

[Install]
WantedBy=timers.target

Then to enable the timer:

# systemctl enable rsyncmirror.timer
Created symlink from /etc/systemd/system/timers.target.wants/rsyncmirror.timer to /etc/systemd/system/rsyncmirror.timer.

@sam-github
Copy link
Contributor

Closing as stale, but if anyone wants to take this up feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants