0

Cron job in docker not working

dwong 2 months ago updated by ovidiu 4 weeks ago 2

Hi, I encounter a cron job problem in a docker, the crontab seems not executing my cron job properly. I have the following bash file located here:

/var/www/html/cron/customcron.sh

This file contains two commands, which will index the metadata of the two folders:

php /var/www/html/cron/metadata_index.php myusername /user-files/Multimedia/Photos/2022
php /var/www/html/cron/metadata_index.php myusername /user-files/Multimedia/Photos/2023

The file's access priviledge is follows:

-rwxr-xr-x 1 root root 168 Sep  6 15:06 customcron.sh

I added this task by using "crontab -e", and this is what I added to crontab (retrieved by using "crontab -l"):

* * * * * touch /var/www/html/cron/example.txt
22 * * * * /var/www/html/cron/customcron.sh

For the first touch command I was using that to test whether cron is really executing, it turns out the touch command does create an example.txt in the corresponding folder.

For the customcron.sh, cron seemed not be running properly.

But when I run the commpand in console: 

/var/www/html/cron/customcron.sh

It was executed correctly, the metadata of the two folders would be indexed.

Finally found the method of solving this problem, let me mark it down here.

After reading the error log, I found out that cron cannot use the php command:

/var/www/html/cron/customcron.sh: line 1: php: command not found
/var/www/html/cron/customcron.sh: line 2: php: command not found

I solve it by adding the first two lines of following through using crontab -e

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 22 * * * * /var/www/html/cron/customcron.sh

And it solved the problem.

I assume that when you update the filerun docker container your modifications to the crontab will be lost, that's why you don't edit stuff inside containers which is not mounted to volumes. The solution would be to run your cronjobs on your docker host:

i.e.

0 3 * * * docker exec -w /var/www/html/cron -i filerun-web php process_search_index_queue.php >/dev/null 2>&1