I prefer to stick scheduled tasks in /etc/crontab so I can see at a glance what's scheduled to run, regardless of which user the task runs as.
The only gotcha is that the format isn't validated on save, unlike crontab -e -- so a stray character can quietly break the entire cron.
Is there a way to validate the /etc/crontab format before/after save?
The only reliable way I found is to check the log.
cron
checks/etc/crontab
every minute, and logs a message indicating that it has reloaded it, or that it found an error.So after editing, run this:
Or, to not wait a full minute, but only until the next minute + 5 seconds:
Example output with an error:
Good output:
That's on Debian 8. On other systems, cron might log to a different file.
(I thought I could avoid hunting for the right log file by using systemd's
journalctl -u cron
, but that didn't show me these log entries, and actually seems to have stopped logging cron events 2 days ago for some reason)Another more recent solution is the python script chkcrontab
Wicked cool shell scripts has a shell script that validates crontab files.
You can get the zip archive containing the script here
The script is called verifycron
I found this cool solution here: https://crontab.guru
It doesn't just validate the crontab, it tells you explicitly what and when the crontab will run, and highlights where errors are.
On Ubuntu, it seems like I can just run:
NOTE: this has the side effect of starting this cronjob (thanks @NZD)
If the file is invalid, I will an error, like:
run
crontab -T path/to/crontab
If you want to automatically do it before/after, you can write your own
visudo
style wrapper, such asPersonally, since breaking the crontab is not as bad as breaking sudoers, I think it's fine to just print the message, but you could also everything to a tempfile if you wanted.