Managing ZFS scrub via systemd

1.8k views Asked by At

I would like to automate zpool scrubbing on my ZFS server. I have written the following systemd service as /etc/systemd/system/zfs-scrub.service)

[Unit]
Description=Scrub ZFS tank pool

[Service]
Type=simple
ExecStart=/bin/sh -c 'zpool scrub `zpool list -H -o name`'
RemainAfterExit=yes
ExecStop=/bin/sh -c 'zpool scrub -s `zpool list -H -o name`'

As well as a timer (as /etc/system.d/system/zfs-scrub.timer) :

[Unit]
Description=Run zpool scrub every 1st Saturday

[Timer]
OnCalendar=Sat *-*-* 22:00:00

After having started it a few weeks back, I checked to see if it behaved. It seems that systemd still thinks the service is running, so the timer didn't run.

It seems there is no ExecStatus, so systemd doesn't know that the service completed.

  • Am I missing something ? Should I instead write a script that starts the scrub, greps the zpool status line and catch signals to stop the scrub when systemd signals it ?
  • Is it possible to write a OnCalendar line that means "once per month, only on weekends" ?
1

There are 1 answers

3
airhuff On

To answer your second question, the following can be read as running the first seven days of each month at a minute past midnight, but only if that day is also a Sunday. Thus, it should run the first Sunday of every month.

OnCalendar=Sun *-*-01..07 00:01:00

Regarding your first question, it would be helpful to see what the journal says, rather than just confirming it ran via the zpool status command. I notice that your service unit does not require the zfs.target. Also I would make the service Type a "oneshot" rather than "simple".

EDIT: This should work for you, though you need to start/enable for each pool:

Try this for your [email protected] file:

[Unit]
Description=Scrub ZFS tank pool

[Service]
Type=oneshot
ExecStart=/bin/zpool scrub %i

Then this for your [email protected] file:

[Unit]
Description=Run zpool scrub every 1st Saturday

[Timer]
OnCalendar=Sun *-*-01..07 00:01:00 

You would then start the service via the timer with:

systemctl start zfs-scrub@[pool name].timer
systemctl enable zfs-scrub@[pool name].timer