I have a Debian machine with Docker and several containers running. I need two of these containers to start Monday through Friday at a certain time and stop at another time.
I had created two scripts to do this same thing, this is the one I have tried (and it works if I run it manually) for boot:
#!/bin/bash
cd /opt/containers/traefik/
docker-compose start handbrake
docker-compose start firefox
The other file is similar but instead of docker-compose start it puts docker-compose stop .
Both files have execute permission and, if I launch them manually, they work perfectly. The problem is when putting them in the crontab. I have it like this:
00 14 * * 1,2,3,4,5 /opt/containers/scripts/arranca_cont.sh
30 05 * * 1,2,3,4,5 /opt/containers/scripts/para_cont.sh
With that it does nothing. Neither stop nor start and I'm a bit lost with this issue. What am I doing wrong? Missing?
Thank you very much.
I suspect you have a problem with the variable
PATH
, since itcron
runs the script with aPATH
not necessarily equal to the one you use when you're in the terminal.If it is true, when doing
docker <etc>
, the shell that executes that program would not find its location, since the variable defined by default by the programcron
isPATH="/usr/bin:/bin"
.Maybe your executable
docker
is not found inside one of these directories.For this reason, I would suggest that within your scripts, you define the variable
PATH
in such a way that you can ensure that within this variable is the path of the folder that contains the commands ofdocker
. Just an example:In my case, the program
docker
is inside the folder/usr/bin/
. You can tell by the result of this:Other ways I can think of to do it are:
echo $PATH
, display the content of thePATH
user console variable you normally use, then copy and paste this near the top of your script:export PATH='<el resultado el echo>'
bash
:. /home/<tu usuario>/.bashrc
. If you usezsh
, then it will be the file.zshrc
, etc.In case this does not work for you, investigate by reviewing the system logs related to the program
cron
: