Created
May 9, 2023 23:37
-
-
Save sarnobat/7a2d2ac170151210cd881abcedf9b0d6 to your computer and use it in GitHub Desktop.
Crontab coding standards, file layout conventions etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(master version here: https://serverfault.com/questions/351259/is-there-a-good-layout-convention-for-cron-files/1130792#1130792 ) | |
### 0) Header comment | |
General useful comments that will reduce the chance of jobs not working as expected. | |
``` | |
## Characters to escape: %, $ | |
``` | |
### 1) Order | |
Order your entries by time of the day (morning at the top, evening at bottom) | |
* Rationale: it's easier to find the entry you wish to tweak. | |
### 2) PATH | |
2) Explicitly set the PATH at the top of the file | |
* Rationale: avoid "no such file or directory" errors (particularly if you need GNU versions of tools rather than Mac builtins!) | |
``` | |
SHELL=/bin/zsh | |
PATH=/Volumes/apps/homebrew/Cellar/findutils/4.9.0/libexec/gnubin/:/Volumes/apps/homebrew/Cellar/coreutils/9.1/libexec/gnubin/:/Volumes/apps/homebrew/Cellar/findutils/4.9.0/libexec/gnubin/:/opt/local/bin/: | |
``` | |
### 3) Limit sh /path/to/script.sh | |
Do not call scripts unless you really have to (it's better to call primitives directly in the crontab file) | |
* Rationale: you can quickly find the entry responsible for certain behaviour | |
### 4) Backup crontab | |
Add the following entry (do the same in the root's crontab): | |
``` | |
@weekly crontab -l > ~/crontab.`whoami`.`date -I`.txt | |
``` | |
* Rationale: whenever you reinstall your OS, you can't easily rebuild your crontab. | |
### 5) | tee /tmp/cron_*log | |
Write the output of each entry to a log file on a ramdisk; | |
``` | |
| tee /tmp/cron_*.log | |
| tee /tmp/cron_*.err.log | |
``` | |
Rationale: | |
* easier to find errors in crons (e.g. `tail /tmp/cron_*.err.log`) | |
* junk log files don't accumulate beyond their useful life | |
### 6) tab after cron expression | |
* Rationale: Some cron expressions will be longer than others, so starting all commands in the same character column will look neater (and help you compare different commands) | |
## Other | |
* Start with a blank crontab every year (so that cruft gets weeded out periodically) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment