From 93c699091fc70744d08bf29e973723a8d7489a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 9 Jun 2013 22:46:24 +0200 Subject: [PATCH] new article: use systemd as a cron replacement --- ...use-systemd-as-a-cron-replacement.markdown | 107 ++++++++++++++++++ .../code/cron-replacement/logrotate.service | 8 ++ .../cron-replacement/man-db-update.service | 8 ++ .../cron-replacement/mlocate-update.service | 8 ++ .../cron-replacement/pacman-update.service | 11 ++ .../code/cron-replacement/pkgstats.service | 6 + .../cron-replacement/reflector-update.service | 9 ++ .../code/cron-replacement/timer-daily.target | 3 + .../code/cron-replacement/timer-daily.timer | 10 ++ .../code/cron-replacement/timer-hourly.target | 3 + .../code/cron-replacement/timer-hourly.timer | 10 ++ .../code/cron-replacement/timer-weekly.target | 3 + .../code/cron-replacement/timer-weekly.timer | 10 ++ .../cron-replacement/verify-shadow.service | 7 ++ source/downloads/services.tar | Bin 0 -> 10240 bytes source/downloads/timers.tar | Bin 0 -> 10240 bytes 16 files changed, 203 insertions(+) create mode 100644 source/_posts/2013-06-09-use-systemd-as-a-cron-replacement.markdown create mode 100644 source/downloads/code/cron-replacement/logrotate.service create mode 100644 source/downloads/code/cron-replacement/man-db-update.service create mode 100644 source/downloads/code/cron-replacement/mlocate-update.service create mode 100644 source/downloads/code/cron-replacement/pacman-update.service create mode 100644 source/downloads/code/cron-replacement/pkgstats.service create mode 100644 source/downloads/code/cron-replacement/reflector-update.service create mode 100644 source/downloads/code/cron-replacement/timer-daily.target create mode 100644 source/downloads/code/cron-replacement/timer-daily.timer create mode 100644 source/downloads/code/cron-replacement/timer-hourly.target create mode 100644 source/downloads/code/cron-replacement/timer-hourly.timer create mode 100644 source/downloads/code/cron-replacement/timer-weekly.target create mode 100644 source/downloads/code/cron-replacement/timer-weekly.timer create mode 100644 source/downloads/code/cron-replacement/verify-shadow.service create mode 100644 source/downloads/services.tar create mode 100644 source/downloads/timers.tar diff --git a/source/_posts/2013-06-09-use-systemd-as-a-cron-replacement.markdown b/source/_posts/2013-06-09-use-systemd-as-a-cron-replacement.markdown new file mode 100644 index 0000000..be88567 --- /dev/null +++ b/source/_posts/2013-06-09-use-systemd-as-a-cron-replacement.markdown @@ -0,0 +1,107 @@ +--- +layout: post +title: "Use systemd as a cron replacement" +date: 2013-06-09 18:22 +comments: true +categories: +- systemd +- linux +- cron +description: "HOWTO: Replace cron with systemd" +--- + +Since systemd 197 timer units support calendar time events, which makes systemd a +full cron replacement. Why one would replace the good old cron? Well, because systemd +is good at executing stuff and monitor its state! + +* with the help of journalctl you get last status and logging output, which is a + great thing to debug failing jobs: + +``` +$ systemctl status reflector-update.service +reflector-update.service - "Update pacman's mirrorlist using reflector" + Loaded: loaded +(/etc/systemd/system/timer-weekly.target.wants/reflector-update.service) + Active: inactive (dead) + +Jun 09 17:58:30 higgsboson reflector[30109]: rating http://www.gtlib.gatech.edu/pub/archlinux/ +Jun 09 17:58:30 higgsboson reflector[30109]: rating rsync://rsync.gtlib.gatech.edu/archlinux/ +Jun 09 17:58:30 higgsboson reflector[30109]: rating http://lug.mtu.edu/archlinux/ +Jun 09 17:58:30 higgsboson reflector[30109]: Server Rate Time +... +``` + +* there are a lot of useful [systemd unit options](http://www.freedesktop.org/software/systemd/man/systemd.exec.html) like `IOSchedulingPriority`, `Nice` or `JobTimeoutSec` +* it is possible to let depend units on other services, like mounting the nfs host + before starting the mysql-backup.service or depending on the network.target. + +So let's get it started. The first thing you might want to do, is to replace the +default scripts located in the [runparts](http://superuser.com/questions/402781/what-is-run-parts-in-etc-crontab-and-how-do-i-use-it) +directories /etc/cron.{daily,hourly,monthly,weekly}. + +On my distribution (archlinux) these are logrotate, man-db, shadow and updatedb: +For convenience I created a structure like /etc/cron.\*: + + mkdir /etc/systemd/system/timer-{hourly,daily,weekly}.target.wants + +and added the following timer. + + cd /etc/systemd/system + wget https://blog.higgsboson.tk/downloads/timers.tar + tar -xvf timers.tar && rm timers.tar + +{% include_code /etc/systemd/system/timer-hourly.timer lang:ini cron-replacement/timer-hourly.timer %} +{% include_code /etc/systemd/system/timer-hourly.target lang:ini cron-replacement/timer-hourly.target %} +{% include_code /etc/systemd/system/timer-daily.timer lang:ini cron-replacement/timer-daily.timer %} +{% include_code /etc/systemd/system/timer-daily.target lang:ini cron-replacement/timer-daily.target %} +{% include_code /etc/systemd/system/timer-weekly.timer lang:ini cron-replacement/timer-weekly.timer %} +{% include_code /etc/systemd/system/timer-weekly.target lang:ini cron-replacement/timer-weekly.target %} + +... and enable them: + + systemctl enable timer-hourly.timer + systemctl enable timer-daily.timer + systemctl enable timer-weekly.timer + +These directories work like their cron equivalents, each service file located in +such a directory will be executed at the given time. + +Now move on to the service files. If you're not running Arch, the paths might be different on your system. + + cd /etc/systemd/system + wget https://blog.higgsboson.tk/downloads/services.tar + tar -xvf services.tar && rm services.tar + +{% include_code /etc/systemd/system/timer-daily.target.wants/logrotate.service lang:ini cron-replacement/logrotate.service %} +{% include_code /etc/systemd/system/timer-daily.target.wants/man-db-update.service lang:ini cron-replacement/man-db-update.service %} +{% include_code /etc/systemd/system/timer-daily.target.wants/mlocate-update.service lang:ini cron-replacement/mlocate-update.service %} +{% include_code /etc/systemd/system/timer-daily.target.wants/verify-shadow.service lang:ini cron-replacement/verify-shadow.service %} + +At last but not least you can disable cron: + + systemctl stop cronie && systemctl disable cronie + +If you want to execute at a special calendar events for example "every first day in a month" use the ["OnCalendar=" option](http://www.freedesktop.org/software/systemd/man/systemd.time.html) in the timer file. +example: + +``` ini send-bill.timer +[Unit] +Description=Daily Timer + +[Timer] +OnCalendar=*-*-1 0:0:O +Unit=send-bill.target + +[Install] +WantedBy=basic.target +``` + +That's all for the moment. Have a good time using the power of systemd! + +Below some service files, I use: + +{% include_code /etc/systemd/system/timer-weekly.target.wants/reflector-update.service lang:ini cron-replacement/reflector-update.service %} +{% include_code /etc/systemd/system/timer-weekly.target.wants/pkgstats.service lang:ini cron-replacement/pkgstats.service %} + +[See this link](https://bbs.archlinux.org/viewtopic.php?id=162989) for details about my shell-based pacman notifier +{% include_code /etc/systemd/system/timer-daily.target.wants/pacman-update.service lang:ini cron-replacement/pacman-update.service %} diff --git a/source/downloads/code/cron-replacement/logrotate.service b/source/downloads/code/cron-replacement/logrotate.service new file mode 100644 index 0000000..d267eb7 --- /dev/null +++ b/source/downloads/code/cron-replacement/logrotate.service @@ -0,0 +1,8 @@ +[Unit] +Description=Update man-db + +[Service] +Nice=19 +IOSchedulingClass=2 +IOSchedulingPriority=7 +ExecStart=/usr/bin/logrotate /etc/logrotate.conf diff --git a/source/downloads/code/cron-replacement/man-db-update.service b/source/downloads/code/cron-replacement/man-db-update.service new file mode 100644 index 0000000..51d4094 --- /dev/null +++ b/source/downloads/code/cron-replacement/man-db-update.service @@ -0,0 +1,8 @@ +[Unit] +Description=Update man-db + +[Service] +Nice=19 +IOSchedulingClass=2 +IOSchedulingPriority=7 +ExecStart=/usr/bin/mandb --quiet diff --git a/source/downloads/code/cron-replacement/mlocate-update.service b/source/downloads/code/cron-replacement/mlocate-update.service new file mode 100644 index 0000000..2a38bc4 --- /dev/null +++ b/source/downloads/code/cron-replacement/mlocate-update.service @@ -0,0 +1,8 @@ +[Unit] +Description=Update mlocate database + +[Service] +Nice=19 +IOSchedulingClass=2 +IOSchedulingPriority=7 +ExecStart=/usr/bin/updatedb diff --git a/source/downloads/code/cron-replacement/pacman-update.service b/source/downloads/code/cron-replacement/pacman-update.service new file mode 100644 index 0000000..20fba93 --- /dev/null +++ b/source/downloads/code/cron-replacement/pacman-update.service @@ -0,0 +1,11 @@ +[Unit] +Description=Update pacman's package cache + +[Service] +Nice=19 +Type=oneshot +IOSchedulingClass=2 +IOSchedulingPriority=7 +Environment=CHECKUPDATE_DB=/var/lib/pacman/checkupdate +ExecStartPre=/bin/sh -c "/usr/bin/checkupdates > /var/log/pacman-updates.log" +ExecStart=/usr/bin/pacman --sync --upgrades --downloadonly --noconfirm --dbpath=/var/lib/pacman/checkupdate diff --git a/source/downloads/code/cron-replacement/pkgstats.service b/source/downloads/code/cron-replacement/pkgstats.service new file mode 100644 index 0000000..d3f4a77 --- /dev/null +++ b/source/downloads/code/cron-replacement/pkgstats.service @@ -0,0 +1,6 @@ +[Unit] +Description=Run pkgstats + +[Service] +User=nobody +ExecStart=/usr/bin/pkgstats diff --git a/source/downloads/code/cron-replacement/reflector-update.service b/source/downloads/code/cron-replacement/reflector-update.service new file mode 100644 index 0000000..7d55802 --- /dev/null +++ b/source/downloads/code/cron-replacement/reflector-update.service @@ -0,0 +1,9 @@ +[Unit] +Description="Update pacman's mirrorlist using reflector" + +[Service] +Nice=19 +IOSchedulingClass=2 +IOSchedulingPriority=7 +Type=oneshot +ExecStart=/usr/bin/reflector --verbose -l 5 --sort rate --save /etc/pacman.d/mirrorlist diff --git a/source/downloads/code/cron-replacement/timer-daily.target b/source/downloads/code/cron-replacement/timer-daily.target new file mode 100644 index 0000000..c509e0d --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-daily.target @@ -0,0 +1,3 @@ +[Unit] +Description=Daily Timer Target +StopWhenUnneeded=yes diff --git a/source/downloads/code/cron-replacement/timer-daily.timer b/source/downloads/code/cron-replacement/timer-daily.timer new file mode 100644 index 0000000..0f828f9 --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-daily.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Daily Timer + +[Timer] +OnBootSec=10min +OnUnitActiveSec=1d +Unit=timer-daily.target + +[Install] +WantedBy=basic.target diff --git a/source/downloads/code/cron-replacement/timer-hourly.target b/source/downloads/code/cron-replacement/timer-hourly.target new file mode 100644 index 0000000..01cebae --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-hourly.target @@ -0,0 +1,3 @@ +[Unit] +Description=Hourly Timer Target +StopWhenUnneeded=yes diff --git a/source/downloads/code/cron-replacement/timer-hourly.timer b/source/downloads/code/cron-replacement/timer-hourly.timer new file mode 100644 index 0000000..07c0053 --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-hourly.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Hourly Timer + +[Timer] +OnBootSec=5min +OnUnitActiveSec=1h +Unit=timer-hourly.target + +[Install] +WantedBy=basic.target diff --git a/source/downloads/code/cron-replacement/timer-weekly.target b/source/downloads/code/cron-replacement/timer-weekly.target new file mode 100644 index 0000000..23549f8 --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-weekly.target @@ -0,0 +1,3 @@ +[Unit] +Description=Weekly Timer Target +StopWhenUnneeded=yes diff --git a/source/downloads/code/cron-replacement/timer-weekly.timer b/source/downloads/code/cron-replacement/timer-weekly.timer new file mode 100644 index 0000000..836e287 --- /dev/null +++ b/source/downloads/code/cron-replacement/timer-weekly.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Weekly Timer + +[Timer] +OnBootSec=15min +OnUnitActiveSec=1w +Unit=timer-weekly.target + +[Install] +WantedBy=basic.target diff --git a/source/downloads/code/cron-replacement/verify-shadow.service b/source/downloads/code/cron-replacement/verify-shadow.service new file mode 100644 index 0000000..67925b3 --- /dev/null +++ b/source/downloads/code/cron-replacement/verify-shadow.service @@ -0,0 +1,7 @@ +[Unit] +Description=Verify integrity of password and group files + +[Service] +Type=oneshot +ExecStart=/usr/sbin/pwck -r +ExecStart=/usr/sbin/grpck -r diff --git a/source/downloads/services.tar b/source/downloads/services.tar new file mode 100644 index 0000000000000000000000000000000000000000..096b716eaccf853e5fcc229045c6efc9b54c8488 GIT binary patch literal 10240 zcmeH~&y$ld6vuPsugE_@At^t`11I(1MV+bZWoHj5q%=wsOv2Xw_dbfQ)hX@HE;!@t z%O$iggz)*kZ(i_fCv8{>)f^UH*joCN*!S1a2;(eG zS(+`gj6kp-PSfd@Fg{b~;I%0wUyQW%_%PBrY2AhRe;V>*`ZuPw#tSbO@Z$HXluv)b zUf^ud`>g()|I$C~S!|Lu)d{w{bUU1?S8mC?mbTY>C!Cv;e? zgWz^G=zkZyhb$=AtKi*-Rk@W_*C<`TZiI6M|Kq=pRvD}Op~!=`-(|T1gZU!roQ>8> zM-PcbBk9ZEKNqFZo8a-n;m9cnjQW4qm=e+;?2g&+tWDsm|Kk+)e<%^0=gTYy|1+L2 z^#2t4UYp~B|Az#iuo;N8aB}*cU`Q?4W3DX{k4;C#p#i*xk=|(luoo))B z4y`PVmTqhO--X}xmA~DWUukI1#%kLR!IQP&<@^Lj{XZQ4rYQf{{eS%a`&s|{??X5X zEHm`~{OS{O#{V;){~@ueH4VdWosu3)|Gy9!pMeCB01`j~NB{{S0VIF~kN^@u0!RP} RAOR$R1dsp{Kmsq0zz;~bXM6wv literal 0 HcmV?d00001 diff --git a/source/downloads/timers.tar b/source/downloads/timers.tar new file mode 100644 index 0000000000000000000000000000000000000000..ad0b62d6b2e3a961c9cf1daeff6f46768670d4ef GIT binary patch literal 10240 zcmeI0L2rUE6vsLDQ}6}mTH3;i!;FXRw1XNC#)Fe)l4&teXZ-eSVH(*Q2n3mCj|-ut zE&Sj6zy3hQTai9Q%Xo7fsAc*tl+*cv_2Dz-n6o~9o-q!nLp;JcVGL82^*Vz`q$CD2$UyV+XE zsgy!QA_|WpL-+7{@QgJIs9*jc9F|gLk2T|(bQ{wLod5Lw={m0oh5tFnexUu&m`5F# zH(8|Zz5m?*rTCj%XsctzI1}Ki`tM^zh0859RKS8U|s$9FhBEO!1DU<`3&lR9|=%X|7`%st^BvX