1

I have the following file execute-backup-from-container.sh. The content of this file is:

#!/bin/bash FILE=minime.sql.`date +"%Y%m%d".gz` CONTAINER='mysql_01' SCRIPT_ON_CONTAINER='/container-mysql-dump.sh' ${OUTPUT}=$(docker exec ${CONTAINER} /$SCRIPT_ON_CONTAINER) echo "==============" echo "$CONTAINER:/$FILE" echo "==============" docker cp "$CONTAINER:/$FILE" backup-data/ 

When I run crontab -e I am putting the following:
0 5 * * 1 /home/me/projects/execute-backup-from-container.sh This means that the execute-backup-from-container.sh should be executed every day at 5:00 am.
The problem is that the script is not executed at all.
So what on earth is the problem? Why is it not executed?

3
  • @steeldriver you are right. If I want to make it every day should I have to put * instead of 1? Commented Mar 23, 2018 at 23:52
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! Commented Mar 25, 2018 at 19:01
  • 1
    Also, you have a syntax error in the script: ${OUTPUT}=... should just be OUTPUT=... Commented Mar 27, 2018 at 12:38

3 Answers 3

5

The cron fields corresponding to your entry mean:

minute: 0 hour: 5 day of month: * month: * day of week: 1 command: /home/me/projects/execute-backup-from-container.sh 

which translate in English to: Mondays at 5am (any day of the month, any month).

If you want it to be executed:

every day at 5:00 am

then you want that 5th field to be a *:

0 5 * * * /home/me/projects/execute-backup-from-container.sh 
3
  • 1
    good point there. I modified the 1 into * to be executed every day of the week, but still does not execute the script. Commented Mar 27, 2018 at 12:36
  • is there a cron error message in the logs? Is the script executable? Commented Mar 27, 2018 at 12:37
  • No, there are no error messages in the logs. I have read the logs with ` grep CRON /var/log/syslog` Commented Mar 27, 2018 at 16:34
1

Did you check that the file is set to be executable? Here is an example of marking a script as executable:

$ ls -l test.sh -rw-r--r-- 1 ahill ahill 0 Mar 23 19:30 test.sh $ chmod +x test.sh $ ls -l test.sh -rwxr-xr-x 1 ahill ahill 0 Mar 23 19:30 test.sh 

The next thing to check is the environment. cron jobs inherit no environment by default. The "fix" is discussed here:
How can I run a cron command with existing environmental variables?

One of the reasons that the environment is a big deal is that cron might not even find bash! see: https://www.digitalocean.com/community/questions/why-is-cron-not-running-my-sh-script

If you still cannot figure it out, I would do a test: Change your cron job from:

0 5 * * * /home/me/projects/execute-backup-from-container.sh 

to:

0 5 * * * /home/me/projects/execute-backup-from-container.sh >> ~/script_errors.log 2>&1 

What will happen: the next time cron launches the job, error messages produced will be dumped into the text file script_errors.log. (change the path to the script_errors.log file to whatever seems appropriate, but make sure the path exists, though the file does not need to.)

I also noticed something in the OP that might be the real problem: the word "container." If the script is inside of a Docker container, then this is likely the solution:
https://www.ekito.fr/people/run-a-cron-job-with-docker/

6
  • This answer misses the point that the schedule used in the crontab is wrong. Commented Mar 24, 2018 at 15:29
  • You right... should have looked at that first... but I will leave it here as it might help someone else that is searching. Commented Mar 26, 2018 at 15:06
  • Good point with the permissions. I had already put 777 to execute-backup-from-container.sh file before posting the question, so it does not help that, but I will check the environment variable solution and let you know. Commented Mar 27, 2018 at 12:38
  • So I have used the following environment variables but still not working: HOME=/home/cristian LOGNAME=cristian PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin SHELL=/bin/bash PWD=/home/cristian BASH_ENV=/home/cristian/.profile Commented Mar 27, 2018 at 17:08
  • 1
    I would do a test: Change your cron job from: 0 5 * * * /home/me/projects/execute-backup-from-container.sh to: 0 5 * * * /home/me/projects/execute-backup-from-container.sh >> ~/script_errors.log 2>&1 What will happen: the next time cron launches the job, error messages produced will be dumped into the text file script_errors.log. (change the path to the script_errors.log file to whatever seems appropriate, but make sure the path exists, but he file does not need to.) Commented Mar 28, 2018 at 22:34
0

After keeping into account all the answers, the last issue was in the last line:

docker cp "$CONTAINER:/$FILE" backup-data/ 

The last line should have been

docker cp "$CONTAINER:/$FILE" docker-projects/mysql/backup-data/ 

Thank you all for your support.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.