tiredofit/docker-nginx-php-fpm

How to add to the crontab

chuckienorton opened this issue · 3 comments

Hello,

I see that cron is automatically starting up in the container, and i'm wondering what the best way to add to the crontab is?

I have tried adding the following to our Dockerfile, in the hopes of kicking off laravel's task scheduler.

# CRONTAB
ARG CRONTAB_DESTINATION=/etc/cron.d/crontab
COPY ./docker/staging/crontab ${CRONTAB_DESTINATION}

And here is what is in that crontab:
* * * * * php /www/html/artisan schedule:run >> /dev/stdout 2>&1

However, when the server boots up, crontab -l only shows the 'logrotate' cron item.

Is there a different way I should be adding to the crontab?

Thanks in advance!!

No worries - a couple different ways you can do this..

The slow way:
Make a file and drop it in /assets/cron/ = ($ASSETS_SCHEDULING_LOCATION) ie artisan with the content of your cron file. I'd highly recommend based on your example there it be written as such:

* * * * * sudo -u ${NGINX_USER} php ${NGINX_WEBROOT}/artisan schedule:run >> /dev/stdout 2>&1

This just allows you to play with the images environment variables better overall. You will get the same results based on your hardcoded this way.

The streamlined way:
Create an environment variable called CRON_ARTISAN="* * * * * php /www/html/artisan schedule:run >> /dev/stdout 2>&1" and it will automatically get written to the crontab automatically. This version sometimes gets a bit weird with environment variables and quotes, but is a great alternative.

I also have a different way where embarrassingly I wrote my own scheduler that allows one to:

Execute something at the set time (ie 12:45pm) and Execute it for the second time a minute later (ie 12:46pm)
Or
Execute something as soon as the container starts - and then execute it for the second time a defined time later.
Or
Execute something 15 minutes after container has started, and then follow the above scheduled sequence.

If you need something of that calibre let me know and I'll add it to the list of getting inside this image.

I believe i got this working. I went with the CRON_ARTISAN variable only because we are using the same repo for our task scheduler (which will need the cron artisan tasks running) and our public application (where we don't want cron artisan tasks running). The variable allows us to add it to one, and leave off for the other.

Thanks so much!!!

Hi Dave, what can i do if i would like put more than one line to crontag?