66

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?

2
  • 6
    If, like me, you came here looking for a simple (non-scriptable) way to tell you whether you'd got your cron entry right, you might be interested in cronchecker.net — it's an online validator for cron entries with human-friendly output. Commented Dec 20, 2014 at 11:18
  • (The last pending edit is mine - I was unaware that I was logged out - sorry) Commented Apr 23 at 8:03

7 Answers 7

48

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:

sleep 60; grep crontab /var/log/syslog | tail

Or, to not wait a full minute, but only until the next minute + 5 seconds:

sleep $(( 60 - $(date +%S) + 5 )) && grep cron /var/log/syslog | tail

Example output with an error:

Jan  9 19:10:57 r530a cron[107258]: Error: bad minute; while reading /etc/crontab
Jan  9 19:10:57 r530a cron[107258]: (*system*) ERROR (Syntax error, this crontab file will be ignored)

Good output:

Jan  9 19:19:01 r530a cron[107258]: (*system*) RELOAD (/etc/crontab)

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)

3
  • 1
    Tested on FreeBSD 10.3. Works like a charm, just replace log path with /var/log/cron
    – Jette
    Commented Aug 5, 2019 at 8:15
  • Path to cron log in Rocky 9 is also /var/log/cron like FreeBSD 10.3.
    – Gwi7d31
    Commented Jul 24, 2023 at 14:33
  • There's no /var/log/syslog in my docker container
    – IronSean
    Commented Jan 2 at 22:19
16

Another more recent solution is the python script chkcrontab

2
  • 2
    It doesn't check users' cronjobs yet
    – userlond
    Commented Oct 19, 2015 at 7:22
  • It told me that cron wouldn't run the file because it contains dots in the file name. Thanks!
    – cweiske
    Commented Aug 19, 2021 at 7:26
11

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

2
  • 1
    Link doesn't work anymore as of 11 jan 2017.
    – Epskampie
    Commented Jan 11, 2017 at 11:37
  • Someone fixed the link. In the zip-file it is located in "wicked_cool_shell_scripts_2e-master/6/48-verifycron". Use it with ./48-verifycron /var/spool/cron/crontabs/root to verify your user root crontab-file.
    – MadMike
    Commented May 25, 2021 at 12:08
6

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.

1

This has worked the best for me:

systemctl restart cron; journalctl -u cron --lines 10

Forcing the restart ensures that cron checks the syntax immediately, instead of waiting 60 seconds. If there are any errors, journalctl will show them.

0

Tailing the logs is the best solution I feel, as pointed out by @mivk in their answer.

journalctl is a good option too:

vedant@zebronics:~$ journalctl -u cron.service 
... <hit shift+G to goto last line>
Nov 03 10:33:01 zebronics cron[627]: (*system*) RELOAD (/etc/crontab)

(Hit shift+G to go the latest line in journal)

0

Try running crontab -T path/to/crontab.

Note that crontab -T may not be available in Debian-based distributions. It's available in Arch Linux.

If you want to automatically do it before/after, you can write your own visudo style wrapper, such as

$EDITOR /etc/crontab
crontab -T /etc/crontab

Personally, 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.

8
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Nov 9, 2021 at 18:13
  • 2
    What OS is this valid for? My crontab command on Ubuntu does not have the -T flag at all
    – MightyPork
    Commented Jun 9, 2023 at 15:13
  • This is from "cronie" cron by Paul Vixie and Colin Dean. I am on Arch Linux. Info is from my man page. Commented Jun 9, 2023 at 18:52
  • The -T option is documented in the Arch Linux man page, but it's not documented in Debian's... apparently Debian chose to omit this option - for their own reasons. In any case, I personally don't feel that should make anyone (except those who like pulling wings off flies?) feel compelled to downvote as the OP gave no indication of which distro they were using. +1 to counter that slight!
    – Seamus
    Commented Apr 12 at 22:45
  • @Seamus that -T option is probably "only" available in Arch Linux, but it's not documented in Debian since it's not available at all in Debian and Ubuntu distributions. Commented Apr 16 at 7:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .