0

I have set up a cronjob using crontab -e as follows:

12 22 * * * /usr/bin/mysql >> < FILE PATH >

This does not run the mysql command. It only creates a blank file. Whereas mysqldump command is running via cron. What could the problem be?

vstm
  • 12,407
  • 1
  • 51
  • 47
user1180463
  • 247
  • 11
  • 21
  • Off topic; belongs on [sf] or possibly [su] – Jim Garrison Aug 24 '12 at 05:23
  • I'm not so certain of that, Jim. Both cron and MySQL are "software tools commonly used by programmers" as per the FAQ. Database _users_ don't mess about with cron jobs, usually only the developers do that. – paxdiablo Aug 24 '12 at 05:29

2 Answers2

2

Surely mysql is the interactive interface into MySQL.

Assuming that you're just running mysql and appending the output to your file with >>, the first time it tries to read from standard input, it will probably get an end-of-file and exit.

Perhaps you might want to think about providing a command for it to process, something like:

12 22 * * * /usr/bin/mysql
                -u me
                -p never_you_mind
                -e "select * from my_table"
                -D my_database
                >>/home/me/output_file

(split across multiple lines for readability, but should be on one line).

As an aside, that's not overly secure since your password may be visible from ps while the process is running. Since it's only an example, I'm not too worried, but you should consider storing the password in a properly secured my.cnf file if you go down this path.

In terms of running a shell script from cron which in turn executes MySQL commands, that should work as well. One choice is with a here-doc:

/usr/bin/mysql -u me -p never_you_mind -D my_database <<EOF
    select * from my_table
    select * from my_other_table where id = 74
EOF
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • So is it not possible to run mysql command via cron ? Actually, I will run mysql command in shell script which will in turn be run via cron. – user1180463 Aug 24 '12 at 05:22
  • No, it is possible, I've updated the answer to show how (from memory). What's not possible is doing it _interactively_ from `cron`. – paxdiablo Aug 24 '12 at 05:24
0
12 22 * * * /usr/bin/mysql >> < FILE PATH > 2>&1

Redirect your error message to the same file so you can debug it.

There is also a good article about how to debug cron jobs: How to debug a broken cron job

Q. Qiao
  • 767
  • 6
  • 17