0

I checked on internet , this might be a common problem, but I dont know how to solve it, all things that they said are not my case...

The cron is running, I checked, still, I have no log files (at least not in var/log) , I am using Debian 6, my .sh works great, I try it manually but in the cron does not work...

here is my cron code (I use crontab -e and later I restart cron service,even if I think its not necessary..)

14 12  * * * /root/mysqldump.sh > /root/MySQLdump.log

the time its just a test I did some minutes ago... when I try it manually the mysqldump.sh works great, but not at the cron... any other idea?

by the way, what does the 2>&1 do??

Thanks in advance

EDIT:

I enable cron log, and now I checked my log file, it is something like this

Feb 11 13:05:01 myserver /USR/SBIN/CRON[5652]: (root) CMD ("/home/dump/sqldump.sh")

but just that, nothing more...

user156355
  • 99
  • 1
  • 3
  • 9
  • What does the log file show? Do you receive any email output from cron? (add this to your question) – James O'Gorman Feb 11 '13 at 18:27
  • What user is the crontab setup for? What log files are you looking for? What's the OS? – mgorven Feb 11 '13 at 18:27
  • Hi, @JamesO'Gorman there are no log files, its weird, I dont know where else find log files for the cron. – user156355 Feb 11 '13 at 18:57
  • @mgorven I am using Debian 6, I set the cron logged as root. I dont need the log files to be honest, only log file sof the cron to know if was done or not – user156355 Feb 11 '13 at 18:58
  • 1
    What are "all things that they said are not [your] case", rather than trying to make us guess? :D – nickgrim Feb 11 '13 at 18:59
  • @nickgrim in other posts I read things like "set cron as root" "check for execute permissions for the file" "check cron is running" but none of them – user156355 Feb 11 '13 at 19:08
  • Did you check /var/log/syslog for entries from CRON e.g. `grep CRON /var/log/syslog` – user9517 Feb 11 '13 at 19:08

3 Answers3

1

The usual reason for scripts working as a particular user but failing under cron is because cron runs with a restricted environment, specifically a minimal PATH.

Your script - as well as your crontab entry - needs to refer to other executables/scripts with the full path e.g. use /usr/bin/mysqldump rather than just mysqldump.


EDIT: Based on your comments, you should change your script to something like the following i.e. ensure you use full paths to mysqldump, gzip and date:

#!/bin/bash
DATE=`/bin/date +"db-%d-%m-%Y"`
/usr/bin/mysqldump -u user -ppassword mydb | /bin/gzip > /home/dump/$DATE.sql.gz
nickgrim
  • 4,466
  • 1
  • 19
  • 28
  • Thanks, I am using /root/mysqldump.sh, its the full path.. works good, If i move to other folder like /var/www and I try execute it from there I can execute it. – user156355 Feb 11 '13 at 19:12
  • What about the **contents** of `/root/mysqldump.sh`? (Also, your log says `/home/dump/sqldump.sh`) – nickgrim Feb 11 '13 at 19:13
  • Its empty... but I have other cron to copy files and it does work when I execute it, but again, as cron does not work... – user156355 Feb 11 '13 at 19:15
  • 1
    I'm confused. You're getting `cron` to run an empty script, and you're expecting it to do something? – nickgrim Feb 11 '13 at 19:17
  • sorry, I was editing, the log file is empty... the .sh has something like this mysqldump -u user-ppassword mydb| gzip > /home/dump/`date +"db-%d-%m-%Y"`.sql.gz ... and works great... – user156355 Feb 11 '13 at 19:22
  • Right, so that's what I'm saying in my answer; your script should use the full path to both `mysqldump` and `gzip` (edit: and `date`) – nickgrim Feb 11 '13 at 19:24
  • full path for my .gz?? I have /home/dump/mydbdate.sql.gz ... is it ok? – user156355 Feb 11 '13 at 19:27
  • No, full path to the commands you are executing in the script. Not 'mysqldump' but the full path to where 'mysqldump' lives on your system. From the shell type 'which mysqldump' and replace 'mysqldump' with the output of that command in your script. –  Feb 11 '13 at 19:37
  • Thank you so much! I didnt get it about "full path"thought were talking about the files,nt the functions,could u please help me with my other cron? I want to upload it with a FTP but when I run it it says something like "local ./dbdate.sql.gz : No such file or directory"...my cron is with the full path to the functions, but I dont know how to handle the full path now.. DATE=`/bin/date +"db-%d-%m-%Y"` FTP=/usr/bin/ftp USERNAME="user" PASSWORD="pass" SERVER="server" FILE="home/dump/"$DATE."sql.gz" BACKUPDIR="/" $FTP -n -i < – user156355 Feb 11 '13 at 20:02
  • You should ask this as another question. :) – nickgrim Feb 11 '13 at 20:04
0

I think its the quoting that is throwing things off try the following

14 12  * * * "/root/mysqldump.sh > /root/MySQLdump.log"

the 2>&1 redirects the std error to std out

trent
  • 3,114
  • 19
  • 17
  • Thanks, I tried with "" but same result... – user156355 Feb 11 '13 at 19:07
  • Try the following ... create a script with the command that runs. Then call the script from cron without parameters. i.e. /root/cron.sh is executable and contains /root/mysqldump.sh > /root/MySQLdump.log then just call /root/cron.sh from your crontab – trent Feb 11 '13 at 21:09
0

The cron daemon logs to syslog like most services, so you'll find the logs in /var/log/syslog. The log messages for job runs look something like this:

Feb 11 11:09:01 mamma CRON[30555]: (mgorven) CMD (cat /proc/acpi/battery/BAT0/info > /tmp/BAT0-info)
mgorven
  • 30,615
  • 7
  • 79
  • 122
  • Thank you, I checked my logs, my cron log now shows this : Feb 11 13:05:01 myserver /USR/SBIN/CRON[5652]: (root) CMD ("/home/dump/sqldump.sh") – user156355 Feb 11 '13 at 19:12