Cron says "errors in crontab file, cannot install"

I am trying to execute the following command command to create backups of MySQL databases.

When I try to add a command to my crontab using crontab -e

, I get errors "errors in crontab file, cannot install" and asks if I want to try again.

mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "steven@brightbear.net" < output

      

Is there anything I should change in this file? Or should I look into creating a script file and calling that from cron.

Thanks in advance for any help you can provide.

+3


source to share


3 answers


If you have specified that script before crontab -e

, of course it will not agree. The line in the crontab file must start with 5 fields indicating when you want the script to run, as it can be read in the crontab manpage .



On the other hand, most Linux distributions currently have pre-installed capabilities for things that need to be done hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's much easier to just put your script in a file in the appropriate directory and it will be executed in the selected time raster. An added benefit is that you won't have to iterate over everything on one line in these files.

+4


source


Yes; More than anything else, I recommend putting your SQL commands in a shell script and then run the shell script from cron

. (And, as Anew explains, the command sequence is easier to maintain / debug if it is split over multiple lines of comments.) But is that all you were feeding into crontab

? Take a look man crontab

and add fields that indicate when you want to run the command.



0


source


From the crontab (5) man page, it looks like the percent signs (%) have a special meaning, so that's probably where you "run into trouble".

Yes, you have to put your commands in a separate shell script and just call that from the crontab line. It will also make the crontab file easier to read and you can easily format the script to be easier to maintain. And you can check it separately from crontab this way.

0


source







All Articles