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.

I have a couple of cron jobs set up to maintain a local copy of a remote database.

The first one downloads the latest version of the database from the remote machine, which runs every day and is working fine.

The second one imports the downloaded data, however it is not working.

The job is just a simple shell script:

#!/bin/bash
mysql -u root -mypass -h localhost my_db < my_db.sql

The cron task is set up like this:

0 8 * * * bash /home/lampadmin/cron/my_db.sh

If I run the shell script manually, it works OK.

What can I do to find out why it is not working through cron?

share|improve this question
    
did you restart crontab after adding cron job in /etc/crontab? –  tejas Apr 15 '13 at 11:53
    
Do you get any error messages from cron? My guess is that you are falling foul of cron's very spartan environment. Try giving the full path to the mysql binary in the script and see if that helps. –  D_Bye Apr 15 '13 at 11:54
    
Add 2>&1 /var/log/myscript.log at the end of the crontab line to see what happens during the script execution: 0 8 * * * /home/lampadmin/cron/my_db.sh 2>&1 /var/log/myscript.log –  nwildner Apr 15 '13 at 13:00
add comment

2 Answers 2

up vote 1 down vote accepted

remove the bash, just have the following and it should work:

0 8 * * * /home/lampadmin/cron/my_db.sh

also check that my_db.sh is executable.

chmod a+x /home/lampadmin/cron/my_db.sh
share|improve this answer
add comment

I have a bad feeling about making the file executable for all. I would try this one to be close to your first idea.

0 8 * * * /bin/bash /home/lampadmin/cron/my_db.sh

It is always a good idea to run commands with its full path. You never know what you will get otherwise. Therefore, you should also provide the full path to my_db.sql in your file my_db.sh.

But besides that, I have an even worse feeling about setting the password via command line. Every user could see the root password with the aid of ps while your script is running.

I suggest to set the password via the .my.cnf file like this

[mysql]
user=root
password=rootpassword

in the home directory of the appropriate user. If this approach is too general for your requirements, you could put this in an extra file /home/lampadmin/cron/my.cnf and use this settings with

/usr/bin/mysql --defaults-extra-file=/home/lampadin/cron/my.cnf -h localhost mydb < /pathto/my_db.sql

In any case only the user should have access to your my.cnf in order to protect your password. You should use

chmod go-rwx my.cnf

to achieve this.

share|improve this answer
add comment

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.