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?
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?
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
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