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?

Cristian
  • 735
  • 2
  • 7
  • 17

3 Answers3

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
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
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/

Art Hill
  • 141
  • This answer misses the point that the schedule used in the crontab is wrong. – Kusalananda Mar 24 '18 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. – Art Hill Mar 26 '18 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. – Cristian Mar 27 '18 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 – Cristian Mar 27 '18 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.) – Art Hill Mar 28 '18 at 22:34
  • I noticed something in the OP that might be the problem: The word "container." If the script is INSIDE a docker container, I bet this is the answer: https://www.ekito.fr/people/run-a-cron-job-with-docker/ – Art Hill Mar 28 '18 at 22:38
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.

Cristian
  • 735
  • 2
  • 7
  • 17