Skip to main content
removed incorrect remark about extra quotes (they don't hurt); removed incorrect remark about the shell possibly not supporting cd
Source Link
Gilles 'SO- stop being evil'
  • 866.2k
  • 205
  • 1.8k
  • 2.3k

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.

  3. You have quite a few extra quote marks in there. Especially the ones around "/usr/bin/mysql".


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" [email protected] < $statfile

Maybe the reduced shell in cron doesn't like the cd /usr/bin command, so you could try changing that to something like cd /usr/bin && /usr/bin/mysql -e "SHOW SLAVE STATUS \G" all on one line. Although, I've never known mysql to have a dependency on being run from /usr/bin.

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.

  3. You have quite a few extra quote marks in there. Especially the ones around "/usr/bin/mysql".


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" [email protected] < $statfile

Maybe the reduced shell in cron doesn't like the cd /usr/bin command, so you could try changing that to something like cd /usr/bin && /usr/bin/mysql -e "SHOW SLAVE STATUS \G" all on one line. Although, I've never known mysql to have a dependency on being run from /usr/bin.

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" [email protected] < $statfile
Source Link
Tim Kennedy
  • 20.2k
  • 5
  • 42
  • 58

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.

  3. You have quite a few extra quote marks in there. Especially the ones around "/usr/bin/mysql".


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" [email protected] < $statfile

Maybe the reduced shell in cron doesn't like the cd /usr/bin command, so you could try changing that to something like cd /usr/bin && /usr/bin/mysql -e "SHOW SLAVE STATUS \G" all on one line. Although, I've never known mysql to have a dependency on being run from /usr/bin.