Home > OS >  Crontab not executing commands in docker
Crontab not executing commands in docker

Time:05-12

I'm building a web app where instructors provide courses and customers buy them. I needed automation for changing courses statuses. Pretty much just checking some conditions and e.g. closing course when the current time is greater than deadline. I'm using php-symfony, everything is dockerized. So to access my php container, I have to start docker-compose and type docker-compose exec php bash. Well in php I have a command which I can run manually, to check all the courses statuses and possibly do some changes to database if the conditions are met. But to automate this process, I decided to use crontabs. I've tested the command in cmd whether I have all the access to do such thing and it worked, so I just put it into crontab to execute every minute.

* * * * * cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1

The first parts gets to the project folder, the second part opens docker container and runs the command that checks courses statuses. In cmd the whole command worked. And even in /var/log/syslog which is supposed to be default crontab logging file, it gave me this output:

May  8 16:42:01 martin-ubuntu CRON[1032921]: (martin) CMD (cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1)

I don't see any visible errors but looking in the database, it didn't change any values while it should.

So I was thinking it could be something with docker? I tried to add second command in crontab which is:

* * * * * cd /home && echo "hi" > a.txt

And it was saving "hi" into given file every minute. So while the first command doesn't work, this one does. Any ideas where is the hidden problem?

CodePudding user response:

change your crontab into this

* * * * * sudo docker-compose -f /home/martin/PhpstormProjects/bp_project/docker-compose.yml run --rm php sh -c ' bin/console courses-check' >/dev/null 2>&1
  • Related