(gnu services mcron) module provides an interface to
GNU mcron, a daemon to run jobs at scheduled times (see GNU mcron). GNU mcron is similar to the traditional Unix
cron daemon; the main difference is that it is implemented in
Guile Scheme, which provides a lot of flexibility when specifying the
scheduling of jobs and their actions.
The example below defines an operating system that runs the
updatedb (see Invoking updatedb in Finding Files) and
guix gc commands (see 调用guix gc) daily, as well as
mkid command on behalf of an unprivileged user (see mkid invocation in ID Database Utilities). It uses gexps to introduce
job definitions that are passed to mcron (see G-表达式).
(use-modules (guix) (gnu) (gnu services mcron)) (use-package-modules base idutils) (define updatedb-job ;; Run 'updatedb' at 3AM every day. Here we write the ;; job's action as a Scheme procedure. #~(job '(next-hour '(3)) (lambda () (execl (string-append #$findutils "/bin/updatedb") "updatedb" "--prunepaths=/tmp /var/tmp /gnu/store")) "updatedb")) (define garbage-collector-job ;; Collect garbage 5 minutes after midnight every day. ;; The job's action is a shell command. #~(job "5 0 * * *" ;Vixie cron syntax "guix gc -F 1G")) (define idutils-job ;; Update the index database as user "charlie" at 12:15PM ;; and 19:15PM. This runs from the user's home directory. #~(job '(next-minute-from (next-hour '(12 19)) '(15)) (string-append #$idutils "/bin/mkid src") #:user "charlie")) (operating-system ;; … ;; %BASE-SERVICES already includes an instance of ;; 'mcron-service-type', which we extend with additional ;; jobs using 'simple-service'. (services (cons (simple-service 'my-cron-jobs mcron-service-type (list garbage-collector-job updatedb-job idutils-job)) %base-services)))
Tip: When providing the action of a job specification as a procedure, you should provide an explicit name for the job via the optional 3rd argument as done in the
updatedb-jobexample above. Otherwise, the job would appear as “Lambda function” in the output of
herd schedule mcron, which is not nearly descriptive enough!
For more complex jobs defined in Scheme where you need control over the top
level, for instance to introduce a
use-modules form, you can move
your code to a separate program using the
program-file procedure of
(guix gexp) module (see G-表达式). The example below
(define %battery-alert-job ;; Beep when the battery percentage falls below %MIN-LEVEL. #~(job '(next-minute (range 0 60 1)) #$(program-file "battery-alert.scm" (with-imported-modules (source-module-closure '((guix build utils))) #~(begin (use-modules (guix build utils) (ice-9 popen) (ice-9 regex) (ice-9 textual-ports) (srfi srfi-2)) (define %min-level 20) (setenv "LC_ALL" "C") ;ensure English output (and-let* ((input-pipe (open-pipe* OPEN_READ #$(file-append acpi "/bin/acpi"))) (output (get-string-all input-pipe)) (m (string-match "Discharging, ([0-9]+)%" output)) (level (string->number (match:substring m 1))) ((< level %min-level))) (format #t "warning: Battery level is low (~a%)~%" level) (invoke #$(file-append beep "/bin/beep") "-r5")))))))
See mcron job specifications in GNU mcron, for more information on mcron job specifications. Below is the reference of the mcron service.
On a running system, you can use the
schedule action of the service
to visualize the mcron jobs that will be executed next:
# herd schedule mcron
The example above lists the next five tasks that will be executed, but you can also specify the number of tasks to display:
# herd schedule mcron 10
This is the type of the
mcron service, whose value is an
This service type can be the target of a service extension that provides additional job specifications (see 合成服务). In other words, it is possible to define services that provide additional mcron jobs to run.
Data type representing the configuration of mcron.
The mcron package to use.
This is a list of gexps (see G-表达式), where each gexp corresponds to an mcron job specification (see mcron job specifications in GNU mcron).