#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Madison
    Posts
    419
    Rep Power
    36

    Shell script doesn't work as cron job


    This code works correctly when executed from the command line, but when I have this entry in my crontab file the output is an empty bz2 file.

    cron tab:
    Code:
    @daily /usr/home/wha/scripts/rperry_wha_bkup.sh

    shell script:
    Code:
    #!/bin/sh
    # Script name : auto_mysql_dump.sh
    # Backup the dbname database
    dir=`date +%Y-%m-%d`
    dbname='rperry_wha'
    if [ -d /home/wha/backup ]; then
    mkdir /home/wha/backup/$dir
    mysqldump -B --user=rperryFile --password=somepassword --host=127.0.0.1 $dbname | bzip2 -c > /home/wha/backup/$dir/$dbname.sql.bz2
    fi
    # End of script auto_mysql_dump.sh
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Use absolute path names.

    Cron has got a different environment setup than your shell. The problem is most likely the PATH variable.

    M.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2004
    Posts
    2,102
    Rep Power
    1767
    this may not apply...so FWIW...

    If you used a 'Windows' (or other non-nix) system to compose the shell script and then uploaded it to the server there is a possibility that there are hidden characters that get ignored when the script is run from bash but cron see it as a corrupted file...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Madison
    Posts
    419
    Rep Power
    36
    Hmm, I tried to be careful with the path names, but maybe mysqldump is the issue. I'll use a full path on it and see what happens.

    Thanks.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Yes, that's what I meant.
    The "PATH" variable is the search path for binaries.
    It does not include /usr/local/bin by default. You can change this, but I prefer to use absolute paths still.

    M.

IMN logo majestic logo threadwatch logo seochat tools logo