Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As per the monitoring of my mysql replication status. I wrote a simple shell script with the following code

#!/bin/bash
date > /tmp/mysql_repl_status.txt
cd /usr/bin/
"/usr/bin/mysql" "-e" "SHOW SLAVE STATUS \G" >> /tmp/mysql_repl_status.txt
mail -s "Netspective MySQL replication status" name@abc.com < /tmp/mysql_repl_status.txt

The issue is when i execute this script manually it works fine, but using cron the script not works.

Using cron got the mail with the output of date command only. What is wrong with my side?

share|improve this question
1  
what is the entry in the crontab? –  zzapper Jun 19 at 12:41
2  
Does your mysql settings need some password or user ? –  Hastur Jun 19 at 13:11
    
In serverfault there is a generic answer to this issue....is there one in this SE for this problem? –  mdpc Jun 19 at 16:57
    
@mdpc There's a summary in the tag wiki. I can find one answer that mentions shell, environment and percent: unix.stackexchange.com/questions/47941/… –  Gilles Jun 19 at 22:05

2 Answers 2

Several possibilities:

  1. Cron doesn't pass a full user environment to scripts run under cron. So vars like $PATH can be different running under cron than running in a user terminal.

  2. Cron requires a newline at the end of every line, so always keep a blank line at the end of the crontab file.


Maybe specify the full paths in the script, and see if that works to start with.

#!/bin/bash
statfile=/tmp/mysql_repl_status.txt
/bin/date > $statfile
cd /usr/bin
/usr/bin/mysql -e "SHOW SLAVE STATUS \G" >> $statfile
/bin/mail -s "Netspective MySQL Replication Status" name@abc.com < $statfile
share|improve this answer
1  
Quoting the path to a command (point 3) is not an issue. It's how you run a command with spaces or other special characters (which should never happen in a sensible system, but you know). –  l0b0 Jun 19 at 13:53
    
Thanks, @l0b0 . I guess it just looks funny to me. :) –  Tim Kennedy Jun 19 at 14:02
    
I would also add 2>&1 to the end of the mysql command line to pick up any error messages. –  Doug O'Neal Jun 19 at 18:57
    
@DougO'Neal It would be better to configure mail properly so that cron's email doesn't get lost. If a cron job emits anything on stdout or stderr, cron sends the output in an email. –  Gilles Jun 19 at 22:06
    
The script is executed under bash, not under a “reduced shell”. And anyway cd /usr/bin is supported by all shells. –  Gilles Jun 19 at 22:08

Cron jobs are run with very little context. If you have a .my.cnf file in your home directory, that could contain the authentication details needed for the mysql command to work. You probably also need the full path to mail - see what which mail prints.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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