How can I check cron syntax on a script like "crontab -e"?

Let's say I write cron files via a script and modify them right under /var/spool/cron/crontabs/

. When using the command, crontab -e

crontab checks the syntax when exiting the editor. Is there a way to do the same check through a script?

+3


source to share


2 answers


Crontab with the -e option opens the default editor with the current cron file and installs it upon exit.

First of all, save your actual cron file so you don't lose or break anything.

crontab -l > backup.cron

      

You can directly install the file you prepared:

crontab yourFile.text

      



Or use a pipe in your script:

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

      

You will receive error messages in case of bad formatting.

More information: man crontab

+4


source


You can check your crontab for non-printable characters (often the cause of broken crontabs) by typing

crontab -l | cat -e

      

or



sudo crontab -l | cat -e

      

if you have a root crontab.

0


source







All Articles