Installation
Install and enable the cron daemon.
sudo pacman -S cronie
sudo systemctl enable --now cronie.service
systemctl status cronie.service # should be active (running)
Setup
To edit crontab, run
crontab -e
If you get an error like this,
/bin/sh: line 1: /usr/bin/vi: No such file or directory
crontab: "/usr/bin/vi" exited with status 127
pass the following commands:
set -Ux EDITOR emacs
set -Ux VISUAL emacs
This defaults to emacs across different Fish sessions.
Then with crontab -e add jobs like this.
SHELL=/usr/bin/fish
PATH=/usr/local/bin:/usr/bin:/bin
0 * * * * /usr/bin/emacs --batch -Q -l "$HOME/Code/monitor.el" >> "$HOME/Code/monitor.log" 2>&1
Fish functions/aliases in ~/.config/fish/functions/ also work.
Syntax
Here is what each * refers to.
| Field | Range | Meaning |
|---|---|---|
| minute | 0–59 | |
| hour | 0–23 | |
| day of month | 1–31 | |
| month | 1–12 | |
| day of week | 0–7 (0 or 7 = Sun) | |
| command | — | run by the SHELL above |
Examples:
| Schedule | Expression |
|---|---|
| every 5 min | */5 * * * * |
| daily 09:00 | 0 9 * * * |
| weekdays 09:00 | 0 9 * * 1-5 |
| 1st of month | 0 0 1 * * |
Shortcuts include @reboot, @hourly, @daily, @midnight, @weekly, @monthly, @yearly.
For example, the above command can be rewritten as
@hourly /usr/bin/emacs --batch -Q -l "$HOME/Code/monitor.el" >> "$HOME/Code/monitor.log" 2>&1
Useful commands:
crontab -e # edit your crontab
crontab -l # list your crontab
crontab -r # remove your crontab
journalctl -t CRON --since today # debug: did cron fire the job?
Note
The crontab resided in /var/spool/cron/<your-username>. To view it in the terminal run this.
sudo cat /var/spool/cron/$(whoami)
Do not edit this file. Always use crontab -e.
Also a brief note on the terms used above:
| Term | What it is | Analogy |
|---|---|---|
| cron | The concept — a time-based job scheduler in Unix. | “the engine” |
| cronie | A specific implementation of the cron daemon. | “one particular engine” |
| crontab | The file (a plain-text table of schedules) and the command (crontab -e, crontab -l used to edit it. |
“the instruction sheet” |