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
  • 1
  • 2
  • 2
    Does this answer your question? [Why is my crontab not working, and how can I troubleshoot it?](https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it) – HBruijn Jul 21 '23 at 06:27
  • 1
    Notably the section regarding percent signs in the linked duplicate – HBruijn Jul 21 '23 at 06:28
  • I read it and I tried some things, but to no avail. – Centaro Jul 22 '23 at 09:30

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
  • 5,263
  • 4
  • 20
  • 26
  • Useful to improve set-up. Thanks, @Romeo. Unfortunatelly, it still threw the same error at me. – Centaro Jul 21 '23 at 07:13
  • @Centaro did you test it to run the things from command line? – Romeo Ninov Jul 21 '23 at 07:26
  • yes and it worked. That's why I think my code is correct. – Centaro Jul 21 '23 at 07:28
  • @Centaro, will edit my script, please check – Romeo Ninov Jul 21 '23 at 07:43
  • Hello again, I have tried your code, but no jobs from the mysql user crontab happen to work. The code works from my user's crontab when I point to the script, but not when I point to it from the mysql crontab. I have tried with and without the source line. Any ideas what might be going on? Thanks, everyone. – Centaro Jul 22 '23 at 09:07
  • @Centaro, eventually do you have in your password symbols which may be interpreted by bash? Try to put passw0rd in `my.cnf` file in home directory (will edit my question) – Romeo Ninov Jul 22 '23 at 09:12
  • The command stops working properly even from my regular user's crontab if I call the password from ~/.my.cnf Thank you so much, that you keep providing ideas. I will certainly try anything else you suggest. But also, I don't see how changing the location of the password would help getting the command to run from the mysql user's crontab. Since it works flawlessly from the regular user's crontab. – Centaro Jul 22 '23 at 10:55
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
  • 46,944
  • 3
  • 83
  • 129
  • I have tried running it escaping the % signs as a cronjob and in a script the cronjob points to, but it didn't run the code. ``` mariadb-dump -u root -pMyPassword --lock-tables -A > /absolute/path/"`date +"\%Y-\%m-\%d"`".sql && rm /absolute/path/"`date -d '-2 days' '+\%Y-\%m-\%d'`".sql ``` I know it still has the password in it, but I will worry about that after I get it to run. – Centaro Jul 21 '23 at 07:21
  • Also, when I run it from cron, I get a different log: ``` lip 21 09:19:01 T15 crond[722]: (root) CAN'T OPEN (/etc/crontab): No such file or directory lip 21 09:19:01 T15 crond[722]: (mysql) RELOAD (/var/spool/cron/mysql) lip 21 09:19:01 T15 crond[268631]: pam_unix(crond:session): session opened for user mysql(uid=964) by mysql(uid=0) lip 21 09:19:02 T15 CROND[268631]: pam_unix(crond:session): session closed for user mysql lip 21 09:19:51 T15 crontab[268660]: (root) LIST (mysql) ``` – Centaro Jul 21 '23 at 07:24