1

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?

3
  • did you restart crontab after adding cron job in /etc/crontab?
    – tejas
    Commented Apr 15, 2013 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
    Commented Apr 15, 2013 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
    – user34720
    Commented Apr 15, 2013 at 13:00

2 Answers 2

1

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
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .