Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check cron syntax by script like "crontab -e" does?

Let´s say i write cron files via script and modify them direct under /var/spool/cron/crontabs/. When using the command crontab -e crontab checks the syntax when i exit the editor. Is there any way, to do the same check via script?

like image 457
michabbb Avatar asked Jul 21 '15 18:07

michabbb


People also ask

For what crontab e command is used?

To submit a cron job, specify the crontab command with the -e flag. The crontab command invokes an editing session that allows you to create a crontab file. You create entries for each cron job in this file. Each entry must be in a form acceptable to the cron daemon.

What is the meaning of * * * * * In crontab?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


2 Answers

Crontab with -e option does open your default editor with the current cron file and installs it after exiting.

First of all, save your actual cron file to be sure of not losing or breaking anything.

crontab -l > backup.cron

You can directly install a file that you have cooked:

crontab yourFile.text

Or use a pipe in a script:

#/bin/bash
Variable="your scheduled tasks"
echo $Variable | crontab

You will get the error messages in the case of bad formatting.

More info: man crontab

like image 90
blashser Avatar answered Oct 28 '22 12:10

blashser


Use chkcrontab:

pip3 install chkcrontab
chkcrontab /etc/cron.d/power-schedule
Checking correctness of /etc/cron.d/power-schedule
E: 15: 0 12 * foo * * root echo hi
e:     FIELD_VALUE_ERROR: foo is not valid for field "month" (foo)
e:     INVALID_USER: Invalid username "*"
E: There were 2 errors and 0 warnings.

Or try a shell script 48-verifycron from Wicked Cool Shell Scripts, 2nd Edition, but it's not as good.

like image 21
stacker-baka Avatar answered Oct 28 '22 10:10

stacker-baka