How to use a systemd timer instead of cron to automate a git pull
-
I have a directory that a git repo is checked out to
/tftpboot
If I am in that directory and use
git pull
, all is as I want it. Great.
Now I want to schedulegit pull
to run every hour.I could use
cron
easily enough, but I want to get more current and usesystemd
as it is the current control mechanism.Reading these results:
https://wiki.archlinux.org/index.php/Systemd/Timers
https://www.certdepot.net/rhel7-use-systemd-timers/I learn i need to use a service file and a timer file. Easy enough. Here is what you do.
First create the .service file in
/etc/systemd/system
# I like nano, use vi if you want nano /etc/systemd/system/gitpull.service
In that file, you need this
[Unit] Description=update /tftpboot with git pull [Service] Type=simple ExecStart=/bin/git --git-dir=/tftpboot/.git --work-tree=/tftpboot/ pull [Install] WantedBy=multi-user.target
That
git
command is extra annoying because CentOS 7 is still on git version 1.8.3.x and the cleaner -C switch doesn't arrive until git 1.8.5.Anyway, next create a
.timer
file also in/etc/systemd/system
nano /etc/systemd/system/gitpull.timer
Put this information in it.
[Unit] Description=Execute git pull every hour on the hour [Timer] OnCalendar=*-*-* *:00:00 Unit=gitpull.service [Install] WantedBy=multi-user.target
The ArchLinux wiki link above explains the syntax for
OnCalendar
Once you have these two files, you simply enable and start it with
systemctl
just like anything else.systemctl enable gitpull.timer systemctl start gitpull.timer
Special thanks to @stacksofplates for his advice via Telegram
-
Good info. I can see the benefits of running things this way with SaltStack!
Time to change some States. -
Could that be one of the reasons why Fedora 28 minimal doesn’t include cronie because of systemd?
-
I wonder how well systemd would work with nextcloud background jobs instead of cron?
-
@black3dynamite said in How to use a systemd timer instead of cron to automate a git pull:
I wonder how well systemd would work with nextcloud background jobs instead of cron?
Should work fine. I’ll have to try that next time I mess with one.
-
I'd think systemd timers will be the proper way to do scheduled tasks in the future. Seems a lot more flexible than cron at first glance.
-
One of the nice things is you can look at your timers easily.
And the results are viewable in the systemd logs
[root@bpbx ~]# journalctl -u gitpull.service Aug 20 08:00:03 bpbx.domain.com systemd[1]: Started update /tftpboot with git pull. Aug 20 08:00:03 bpbx.domain.com systemd[1]: Starting update /tftpboot with git pull... Aug 20 08:00:11 bpbx.domain.com git[24804]: Already up-to-date. Aug 20 09:00:03 bpbx.domain.com systemd[1]: Started update /tftpboot with git pull. Aug 20 09:00:03 bpbx.domain.com systemd[1]: Starting update /tftpboot with git pull... Aug 20 09:00:03 bpbx.domain.com git[982]: ssh_exchange_identification: Connection closed by remote host Aug 20 09:00:03 bpbx.domain.com systemd[1]: gitpull.service: main process exited, code=exited, status=1/FAILURE Aug 20 09:00:03 bpbx.domain.com git[982]: fatal: Could not read from remote repository. Aug 20 09:00:03 bpbx.domain.com git[982]: Please make sure you have the correct access rights Aug 20 09:00:03 bpbx.domain.com git[982]: and the repository exists. Aug 20 09:00:03 bpbx.domain.com systemd[1]: Unit gitpull.service entered failed state. Aug 20 09:00:03 bpbx.domain.com systemd[1]: gitpull.service failed. Aug 20 10:00:01 bpbx.domain.com systemd[1]: Started update /tftpboot with git pull. Aug 20 10:00:01 bpbx.domain.com systemd[1]: Starting update /tftpboot with git pull... Aug 20 10:00:11 bpbx.domain.com git[9145]: Already up-to-date.
Looks like it failed for some reason at 9am, but ran fine at 8 and 10.
-
@jaredbusch Also if you have cockpit installed, under services, you can easily create and manage timers too.
-
@black3dynamite said in How to use a systemd timer instead of cron to automate a git pull:
@jaredbusch Also if you have cockpit installed, under services, you can easily create and manage timers too.
These specific examples are FreePBX installs. not going to mess a lot with the install.
Though I did have to install git from yum. -
@jaredbusch Very interesting thankyou. I like this way of doing things
-
Oh look I just found this posted here already /sigh..
So many questions I could have not asked of @stacksofplates, had I recalled this thread.
https://mangolassi.it/topic/13455/systemd-timers-instead-of-cron -
And looks like I'm going to have to make my git pull into a scrupt and make it smarter because this is not a great success rate IMO.
-
@jaredbusch said in How to use a systemd timer instead of cron to automate a git pull:
Oh look I just found this posted here already /sigh..
So many questions I could have not asked of @stacksofplates, had I recalled this thread.
https://mangolassi.it/topic/13455/systemd-timers-instead-of-cronI honestly forgot I posted that.