Subject: [PATCH] Add scron service.

From: Robert Vollmert <[email-masked]>
Date: Fri, 29 Nov 2019 19:07:22 +0100

It's a simple replacement for the mcron service.

If you have a mcron job definition like

(define cron-job
  #~(job "*/15 * * * *" #$(program-file ...)))

you can convert it into the valid scron job

(define cron-job
  (scron-job (schedule "/15 * * * *")
             (program-file ...)))

* gnu/services/scron.scm: New file.
* gnu/local.mk: Add it.
---

Sent an incomplete patch before, this is the full version.

 gnu/local.mk           |   1 +
 gnu/services/scron.scm | 101 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 gnu/services/scron.scm

diff --git a/gnu/local.mk b/gnu/local.mk
index 56ff1d0f7b..ca8b6ecc1b 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -552,6 +552,7 @@ GNU_SYSTEM_MODULES =				\
   %D%/services/nix.scm				\
   %D%/services/nfs.scm			\
   %D%/services/pam-mount.scm			\
+  %D%/services/scron.scm			\
   %D%/services/security-token.scm		\
   %D%/services/shepherd.scm			\
   %D%/services/sound.scm			\
diff --git a/gnu/services/scron.scm b/gnu/services/scron.scm
new file mode 100644
index 0000000000..0ea7cc9698
--- /dev/null
+++ b/gnu/services/scron.scm
@@ -0,0 +1,101 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2019 Robert Vollmert <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu services scron)
+  #:use-module (gnu services)
+  #:use-module (gnu services shepherd)
+  #:autoload   (gnu packages suckless) (scron)
+  #:use-module (guix gexp)
+  #:use-module (guix records)
+  #:use-module (guix store)
+  #:use-module (ice-9 match)
+  #:use-module (srfi srfi-1)
+  #:export (scron-configuration
+            scron-configuration?
+            scron-configuration-scron
+            scron-configuration-jobs
+
+            scron-job
+            scron-job?
+            scron-job-schedule
+            scron-job-command
+
+            scron-service-type
+            scron-service))
+
+;;; Commentary:
+;;;
+;;; This module implements a service to run instances of scron, a
+;;; periodic job execution daemon.  Example of a service:
+;;
+;;  (service scron-service-type
+;;           (scron-configuration
+;;            (jobs (list (scron-job (schedule "*/15 * * * *")
+;;                                   (command  "echo hello!"))))))
+;;;
+;;; Code:
+
+(define-record-type* <scron-configuration> scron-configuration
+  make-scron-configuration
+  scron-configuration?
+  (scron             scron-configuration-scron    ;package
+                     (default scron))
+  (jobs              scron-configuration-jobs     ;list of <scron-job>
+                     (default '())))
+
+(define-record-type* <scron-job> scron-job
+  make-scron-job
+  scron-job?
+  (schedule scron-job-schedule (default "* * * * *"))
+  (command  scron-job-command  (default '())))
+
+(define (crontab jobs)
+  (apply mixed-text-file "crontab"
+    (concatenate
+      (map
+        (match-lambda
+          (($ <scron-job> schedule command)
+            (list schedule " " command "\n")))
+        jobs))))
+
+(define scron-shepherd-services
+  (match-lambda
+    (($ <scron-configuration> scron jobs)
+     (list
+       (shepherd-service
+        (provision '(scron))
+        (requirement '(user-processes))
+        (start #~(make-forkexec-constructor
+                  (list (string-append #$scron "/bin/crond")
+                        "-n" ; don't fork
+                        "-f" #$(crontab jobs))
+                  #:log-file "/var/log/scron.log"))
+        (stop #~(make-kill-destructor)))))))
+
+(define scron-service-type
+  (service-type (name 'scron)
+                (extensions
+                 (list (service-extension shepherd-root-service-type
+                                          scron-shepherd-services)))
+                (compose concatenate)
+                (extend (lambda (config jobs)
+                          (scron-configuration
+                           (inherit config)
+                           (jobs (append (scron-configuration-jobs config)
+                                         jobs)))))
+                (default-value (scron-configuration))))
-- 
2.24.0