Notes on cron and anacron in dom0

Following is a general post on what I learned from reading docs and this forums, with respect to cronjobs, cron daemon, the relevant /etc/ folders and the anacron. Feel free to provide feedback on my understanding (and answers to the 2 questions I posted) !

I was reading the TRIM on QubesOS for SSD’s. And for this purpose, as the aforementioned page requested, I created a trim script and placed it on /etc/cron.daily/ folder.

(in dom0) $ journalctl -u crond
Now, checking out the jorunalctl on dom0 for logs regarding cron entries, I am seeing the crond has successfully started. Following that Anacron, too, has been successfully started, and the trim script got executed.

(in dom0) $ sudo fstrim -av
returns 0 B trimmed on both /boot and / partitions.

So, at this point, I would like to note down what I learned with respect to cron processes on QubesOS in dom0 qube.

First of all, there is a 0hourly script under /etc/cron.d/ folder, which runs, every hour, the hourly job scripts placed under /etc/cron.hourly/ folder.

Question 1: Under the same /etc/cron.d/ folder, there is a script named qubes-sync-clock.cron. How does cron in dom0 understand when and in which intervals this script needs to be run? Looking at journalctl -u crond output, I see that script has been run hourly, but I am not seeing what makes this script run hourly. Are the scripts put under /etc/cron.d/ folder run by-default every hour? If so, why is there a specific /etc/cron.hourly/ folder present?

Partial answer 1: Reading on $ man crond reveals that cron checks /etc/cron.d/ folder and /etc/anacrontab file, when the crond starts. So, the /etc/cron.d/qubes-sync-clock.cron script is read when the daemon starts and since this script itself contains a crontab entry specified to run at the 0’th minute of every hour, this qubes-sync-clock.cron job is seen as run-hourly under journalctl -u crond logs in dom0.
I would like to get you guys’ ideas as to why this qubes-sync-clock.cron not placed in the /etc/cron.hourly/ folder, as it would provide consistency (having all hourly cron scripts in one folder named specifically for this purpose).

Moving on. So, /etc/cron.d/0hourly cron script runs every hour and causes the scripts under /etc/cron.hourly/ folder to be run.
Looking at /etc/cron.hourly/ folder, we see a file named 0anacron. This script is a shell script, which looks to see if itself was run today earlier then. If not, then it calls /usr/sbin/anacron -s program and runs it. This causes anacron scheduling utility to be run.

Anacron is similar to cron, but with one difference: cron needs the host machine to be running 24/7 in order for the scheduled jobs to be executed. If there is a cronjob scheduled to run at 15:00 in the afternoon yet the machine isn’t running, then, the cron will not make that job run even if the machine gets turned on at 15:30 o’clock by its user.
Anacron handles such scenarios: anacron makes the scheduled job scripts to be run when the machine gets turned back on, even if the machine was off during the exact schedule time.

So, anacron is good. Anacron is probably what you want when you think about cronjobs.

Moving on: now we know that cron calls the 0hourly script under /etc/cron.d/ folder, which calls the cron scripts to be run under /etc/cron.hourly/ folder, which causes the 0anacron file to be run and thus the anacron utility to be executed.

But what does the anacron do? Reading on $ man anacron we see that “Anacron reads a list of jobs from /etc/anacrontab file.”

Question 2: Since both crond and anacron man-pages mention reading the file /etc/anacrontab how is it decided whether cron or anacron executes the /etc/anacrontab file entries?

So, we look into the /etc/anacrontab file, and we see that it contains lines that runs all the executables in the specified directories of /etc/cron.daily/, /etc/cron.weekly/ and /etc/cron.monthly/.

So, cron → (runs) /etc/cron.d/* → (runs) /etc/cron.hourly/0anacron → (runs) /etc/cron.daily/* && /etc/cron.weekly/* && /etc/cron.monthly/*

So, we see that most of the heavy-lifting will be done by the anacron, using the executable shell scripts that the user places under cron.daily, cron.weekly, and cron.monthly folders.

Question 3: The /etc/anacrontab file specifies “SHELL=/bin/sh”. Does that mean that the shell scripts that one places under /etc/cron.daily/, /etc/cron.weekly/, /etc/cron.monthly/ has to have hashbangs specifying /bin/sh (or /usr/bin/sh) ?
However, I see under dom0 that both of these are symbolic link’ed to /bin/bash or /usr/bin/bash. Does that mean anacron can run #!/bin/bash hashbag’ed scripts? Also, if a non-root user going to be running the bash script, should the hashbang be #!/usr/bin/bash ?
This is a bit confusing on my part.

Anyways, that’s most of what sticked with me, during my readings and hackings of cron on my QubesOS. Feel free to correct my understanding!