Score:0

Why doesn't cron run the commands in another user's crontab?

in flag

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?

HBruijn avatar
in flag
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 avatar
in flag
Notably the section regarding percent signs in the linked duplicate
Centaro avatar
in flag
I read it and I tried some things, but to no avail.
Score:2
in flag

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

Centaro avatar
in flag
Useful to improve set-up. Thanks, @Romeo. Unfortunatelly, it still threw the same error at me.
Romeo Ninov avatar
in flag
@Centaro did you test it to run the things from command line?
Centaro avatar
in flag
yes and it worked. That's why I think my code is correct.
Romeo Ninov avatar
in flag
@Centaro, will edit my script, please check
Centaro avatar
in flag
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.
Romeo Ninov avatar
in flag
@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)
Centaro avatar
in flag
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.
Score:1
jp flag
  • 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.
Centaro avatar
in flag
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 avatar
in flag
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) ```
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.