I have a sh script. How to run it using crontab?
Solution 1
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
Solution 2
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/
Related videos on Youtube
Cristian
Updated on September 18, 2022Comments
-
Cristian over 1 year
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 theexecute-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 about 6 years@steeldriver you are right. If I want to make it every day should I have to put
*
instead of1
? -
Jeff Schaller about 6 yearsIf any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
-
Jeff Schaller about 6 yearsAlso, you have a syntax error in the script:
${OUTPUT}=...
should just beOUTPUT=...
-
-
Kusalananda about 6 yearsThis answer misses the point that the schedule used in the crontab is wrong.
-
Jeff Schaller about 6 yearsis there a cron error message in the logs? Is the script executable?
-
Cristian about 6 yearsGood 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 theenvironment variable
solution and let you know. -
Cristian about 6 yearsNo, there are no error messages in the logs. I have read the logs with ` grep CRON /var/log/syslog`
-
Cristian about 6 yearsSo 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
-
Art Hill about 6 yearsI 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 about 6 yearsI 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: ekito.fr/people/run-a-cron-job-with-docker