We are creating a new cron job under /etc/cron.d.

This cron job has around 56 lines, and all commands should be executed at the start of each month.

I am looking for suggestions to validate the syntax of the cron job.

I mean, how can I verify if the cron job file is configured correctly without mistakes - is there some command which can verify this?

closed as unclear what you're asking by Jeff Schaller, Isaac, msp9011, countermode, Networker Aug 30 '18 at 11:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    if you have to have 56 lines in cron you might be doing something wrong. I would prefer to offload that to a script. – Rui F Ribeiro Aug 29 '18 at 15:05
  • 1
  • 2
    I second Rui’s comment. Beyond that, how much validation are you looking for? crontab -e will validate a crontab before installing it, but I’m not sure that’s easily accessible in other scenarios. It also only validates the cron-specific syntax, it doesn’t validate the command. – Stephen Kitt Aug 29 '18 at 15:07
  • You have 56 entries in the new file, and just want to make sure they are configured to be run on the 1st of each month? – Jeff Schaller Aug 29 '18 at 15:17
  • 1
    Actually crontab in its default replacement mode will also validate the syntax of the crontab that’s provided, but that won’t work for /etc/cron.d-style files. – Stephen Kitt Aug 29 '18 at 15:42

Here's a start to a validator, written in awk, that checks:

  • for non-commented lines
  • for lines with enough fields to look like a crontab entry
  • where the day-of-month value in $3 is not 1 or *
  • where the month value is not *

... then print the (offending) line.

On a sample input of:

53 23 * * * root /usr/lib64/sa/sa2 -A
53 23 1 * * root /usr/lib64/sa/sa2 -A
53 23 2 * * root /usr/lib64/sa/sa2 -A

The output is:

53 23 2 * * root /usr/lib64/sa/sa2 -A

This would have to be enhanced to handle month names and ranges (or even @monthly), as the cron syntax allows.

awk '!/^#/ && NF >= 7 && ($3 != 1 && $3 != "*") && $4 != "*" 1'

Not the answer you're looking for? Browse other questions tagged or ask your own question.