1

I have this cronjob:

#!/bin/bash
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
mkdir -p $YEAR/$MONTH/$DAY
mysqldump -uroot -p1234 locatari > $YEAR/$MONTH/$DAY/backup.sql

And I want to run this at each let's say 3 minutes. I tried with each of these crontab commands and none on them seems to work:

3 * * * * /usr/bin /home/rome/cronjob/back.sh > /home/rome/cronjob
3 * * * * /home/rome/cronjob/back.sh/
3 * * * * exec `/bin/bash /home/rome/cronjob/back.sh`
3 * * * * /bin/bash /home/rome/cronjob/back.sh > /dev/null 2>&1

I only made it run when I did:

bash back.sh

All worked as expected with the bash command, what is wrong to my cron scheduler? Problem is that with the crontab command, I can't see my output, it only works with bash command.

closed as off-topic by roaima, andcoz, slm Jun 22 '18 at 0:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – roaima, andcoz, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

2

None of your crontab job entries matches what you typed at the command line

  1. /usr/bin /home/rome/cronjob/back.sh tries to run a program /usr/bin, but that's a directory
  2. /home/rome/cronjob/back.sh/ tries to treat your script as a directory
  3. exec `/bin/bash /home/rome/cronjob/back.sh` tries to use the output of your program as a program name to run
  4. /bin/bash /home/rome/cronjob/back.sh > /dev/null 2>&1 this might work, except that you have thrown away all the output of the script, so you won't see any errors

Based on feedback from your comments it seems you probably want something like this (I've omitted /home/rome because that is your home directory anyway):

*/3 * * * * cd cronjob && bash back.sh >back.log 2>&1
  • Sorry for the typo in my post, it was: bash back.sh, my bash file is back.sh, but I was using the name properly in my tests, anyway I was testing your last exemple, I got only the file backuplog with the text: mysqldump: [Warning] Using a password on the command line interface can be insecure, the same text runs when I bash back.sh, but the problem is that it won't generate the file, bash back.sh generates. – Tatu Bogdan Jun 21 '18 at 13:05
  • @TatuBogdan what directory do you run the bash back.sh command from, when you do it yourself? (Your home directory? Somewhere else?) – roaima Jun 21 '18 at 13:10
  • cd /home/rome/cronjob and then bash back.sh – Tatu Bogdan Jun 21 '18 at 13:12
  • @TatuBogdan is /home/rome your home directory? Also, have you looked in ~/2018/06/21 for your dump file from crontab? – roaima Jun 21 '18 at 13:15
  • Yes, and that is the full route to my sh file: /home/rome/cronjob, I can't find any created directory. – Tatu Bogdan Jun 21 '18 at 13:25
2

Your cron schedule expression means "At every minute 3" => each hour.
Every 3 minutes would be:

*/3 * * * *

See this nice tool to check the expression.

Also only your last cron command will work. But I would not redirect the output to /dev/null. If there are any problems with your script, it's difficult to debug.

Not the answer you're looking for? Browse other questions tagged or ask your own question.