ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    How to use a systemd timer instead of cron to automate a git pull

    IT Discussion
    systemd timer cron fedora rhel git pull git systemd timers
    6
    13
    3.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by JaredBusch

      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 schedule git pull to run every hour.

      I could use cron easily enough, but I want to get more current and use systemd 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

      1 Reply Last reply Reply Quote 6
      • ObsolesceO
        Obsolesce
        last edited by

        Good info. I can see the benefits of running things this way with SaltStack!
        Time to change some States.

        1 Reply Last reply Reply Quote 1
        • black3dynamiteB
          black3dynamite
          last edited by

          Could that be one of the reasons why Fedora 28 minimal doesn’t include cronie because of systemd?

          1 Reply Last reply Reply Quote 0
          • black3dynamiteB
            black3dynamite
            last edited by

            I wonder how well systemd would work with nextcloud background jobs instead of cron?

            JaredBuschJ 1 Reply Last reply Reply Quote 0
            • JaredBuschJ
              JaredBusch @black3dynamite
              last edited by

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

              1 Reply Last reply Reply Quote 1
              • travisdh1T
                travisdh1
                last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • JaredBuschJ
                  JaredBusch
                  last edited by JaredBusch

                  One of the nice things is you can look at your timers easily.
                  0_1534777832623_b678d47e-5f53-4236-98ae-403adf3e4987-image.png

                  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.

                  black3dynamiteB 1 Reply Last reply Reply Quote 2
                  • black3dynamiteB
                    black3dynamite @JaredBusch
                    last edited by

                    @jaredbusch Also if you have cockpit installed, under services, you can easily create and manage timers too.

                    JaredBuschJ 1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch @black3dynamite
                      last edited by

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

                      jmooreJ 1 Reply Last reply Reply Quote 1
                      • jmooreJ
                        jmoore @JaredBusch
                        last edited by

                        @jaredbusch Very interesting thankyou. I like this way of doing things

                        1 Reply Last reply Reply Quote 0
                        • JaredBuschJ
                          JaredBusch
                          last edited by JaredBusch

                          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

                          stacksofplatesS 1 Reply Last reply Reply Quote 2
                          • JaredBuschJ
                            JaredBusch
                            last edited by

                            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.

                            0_1535130418624_5b1fb4dd-8cd8-4399-b140-b721a0d26ca2-image.png

                            1 Reply Last reply Reply Quote 1
                            • stacksofplatesS
                              stacksofplates @JaredBusch
                              last edited by

                              @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-cron

                              I honestly forgot I posted that.

                              1 Reply Last reply Reply Quote 1
                              • 1 / 1
                              • First post
                                Last post