Cron job not running / not successful?

8,404

Solution 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

Solution 2

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:
8,404

Related videos on Youtube

Dan
Author by

Dan

Updated on September 18, 2022

Comments

  • Dan
    Dan over 1 year

    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?

    • tejas
      tejas about 11 years
      did you restart crontab after adding cron job in /etc/crontab?
    • Jazz
      Jazz about 11 years
      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.
    • Admin
      Admin about 11 years
      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