2

So, I am having some trouble with my cron setup - I am assuming it is a mistake I have made as this is the first time I have set it up. I have two jobs I have setup in cron, one to run at 1am the other at 2am, daily. This is my /etc/crontab:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

0 1 * * * root /usr/local/mysql/bin/mysqldump -usomeuser -psomepassword --opt zabbix > "/backups/zabbix_mysqldb.sql_$(date +%F_%R)"
0 2 * * * root /usr/bin/sh /zabbix_scripts/mysql_backup_script/zabbix-mysql-dump -p somepassword -o /backups

Here is what I am seeing in /var/log/cron:

Feb  7 01:00:01 adlmonitor01 CROND[17334]: (root) CMD (/usr/local/mysql/bin/mysqldump -usomeuser -psomepassword --opt zabbix > "/backups/zabbix_mysqldb.sql_$(date +)
Feb  7 01:01:01 adlmonitor01 CROND[17539]: (root) CMD (run-parts /etc/cron.hourly)
Feb  7 01:01:01 adlmonitor01 run-parts(/etc/cron.hourly)[17539]: starting 0anacron
Feb  7 01:01:01 adlmonitor01 anacron[17548]: Anacron started on 2017-02-07
Feb  7 01:01:01 adlmonitor01 anacron[17548]: Normal exit (0 jobs run)
Feb  7 01:01:01 adlmonitor01 run-parts(/etc/cron.hourly)[17550]: finished 0anacron

Feb  7 02:00:01 adlmonitor01 CROND[28788]: (root) CMD (/usr/bin/sh /zabbix_scripts/mysql_backup_script/zabbix-mysql-dump -p somepassword -o /backups)
Feb  7 02:01:01 adlmonitor01 CROND[28992]: (root) CMD (run-parts /etc/cron.hourly)
Feb  7 02:01:01 adlmonitor01 run-parts(/etc/cron.hourly)[28992]: starting 0anacron
Feb  7 02:01:01 adlmonitor01 anacron[29001]: Anacron started on 2017-02-07
Feb  7 02:01:01 adlmonitor01 anacron[29001]: Normal exit (0 jobs run)
Feb  7 02:01:01 adlmonitor01 run-parts(/etc/cron.hourly)[29003]: finished 0anacron

My guess is a syntax mistake I have made, but I can't seem to figure out where, could someone shed some light?

  • 1
    % symbols split the input unless escaped! Usually simplest to just put a single command into the crontab, which would be a script that does whatever is needed. see man 5 crontab – icarus Feb 8 '17 at 2:14
1

Try replacing the long command invocation and redirected output that you are trying to invoke with a script that does the same thing, e.g. put the line

/usr/local/mysql/bin/mysqldump -usomeuser -psomepassword --opt zabbix > "/backups/zabbix_mysqldb.sql_$(date +%F_%R)"

into a script file, say /root/mytestscript, make it executable, and invoke it in cron as

0 1 * * * root /root/mytestscript > /root/mytestscript.log 2> /root/mytestscript.err

...or, even better, include the output and error logging within the body of mytestscript itself, which allows you to do

0 1 * * * root /root/mytestscript

You'll probably find that cron is swallowing stdout and stderr (or, as in classic cron, attempting to mail you the output). One of the above two invocation methods will ensure you see all the logs in predictable places.

Finally, FYI, it's not secure to put passwords in scripts, so all this is a temporary fix until you solve that problem. Try https://stackoverflow.com/questions/6861355/mysqldump-launched-by-cron-and-password-security/6861458#6861458 for help there.

  • The idea is good, but the redirections should be part of the script rather than in the crontab. – icarus Feb 8 '17 at 2:15
  • Thanks for the information, this fundamentally changes the way I am going to work with cron in the future. I will also checkout the link :). – DRCat Feb 9 '17 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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