0

Hello everyone and thank you in advance.

I have written a job in the mysql user crontab, but it doesn't run at all. I think my code is correct, because it works properly if I execute it pointing to a script from my user's crontab:

00 12 * * * /usr/bin/mariadb-dump -u root -pMyPassword  --lock-tables -A > /full/path/"`date +"%Y-%m-%d"`".sql && rm /full/path/"`date -d '-2 days' '+%Y-%m-%d'`".sql 

But it does not work when I point at it from the mysql user's crontab:

00 12 * * * /full/path/to/script

I have also tried running it directly escaping the % signs, but that didn't help either.

I have tried running the script with and without specifying source, even though I am not sure how that would help:

#!/bin/bash
source /full/path/to/my/user/.zshrc
00 12 * * * /usr/bin/mariadb-dump -u root -pMyPassword  --lock-tables -A > /full/path/"`date +"%Y-%m-%d"`".sql && rm /full/path/"`date -d '-2 days' '+%Y-%m-%d'`".sql

I have also added an extra blank line to the bottom of the crontab. It didn't help, and the journal doesn't tell me much.

I have also tried changing the permissions for the folder and its contents. Now, it's on 775, and I have added the mysql user to the group, but it still doesn't work.

Any ideas what might be wrong with my set-up?

Centaro
  • 11

2 Answers2

2

Usually cron do not handle well the & and other symbols so the best you can do is to create script like. Also consider using absolute paths everywhere:

#!/bin/bash
source /your/home/directory/.bash_profile
/path/to/mariadb-dump -u root -pMyPassword  --lock-tables -A > /path/to/desired/path/"`date +"%Y-%m-%d"`".sql && rm /path/to/desired/path/"`date -d '-2 days' '+%Y-%m-%d'`".sql

and run it from cron:

* 12 * * * /path/to/script

Do not forget to make this script executable. Also run it every minute is not the wise way. Probably you want to run it once per day:

0 12 * * * /path/to/script

If you have special symbols in your password try to create file ~/.my.cnf and add this inside:

[client]
# The following password is sent to all standard MySQL clients
password="my password"

and leave in command line just -p w/o password after this

Romeo Ninov
  • 6,677
1
  • You need to escape % sings with the backslashes \. That's why it doesn't run the command as expected.
  • It might be a good idea to use absolute paths instead of ~/relative/paths.
  • * 12 * * * runs the command on every minute of the hour 12; please revise.
  • Do not put the password on the command line as it will be logged and it will also be visible to every user while the command is running.
Esa Jokinen
  • 52,963
  • 3
  • 95
  • 151