Configure scheduled Tasks in Docker container crontab (Daocloud + docker + Laravel5)

Source: Internet
Author: User
A wild full-stack engineer, likes to study a variety of new technologies.

Follow now

The recent project involves a function of a timed task, so go to these days to study the use of crontab, in accordance with the relevant online tutorials successfully on their own computer to open this feature

Laravel + crontab

Add crontab Configuration

1. Execution of Orders

$ crontab-e

2, add the following (Path/to is the application path), that is, every minute to execute the following command

* * * * * * Php/path/to/artisan schedule:run >>/dev/null 2>&1

3, start (The following command is Ubuntu under the Start command, other systems may not be the same)

$/etc/init.d/cron Start

Complete the above three steps to successfully open the Laravel task scheduling function, the following to verify

Verifying that task scheduling is performing properly

./app\console\kernel

...    protected function Schedule (schedule $schedule)    {        $schedule->call (function () {            //The following code inserts a tweet data            db::table (' tweets ')->insert ([' content ' = ' Hi ']);        }) ->everyminute ();    } ...

After a few minutes, you can check to see if the database has successfully inserted data ~

Reference articles

    • Laravel Task Scheduling
    • How to use the Crontab for planning tasks under Docker [Python]
    • Laravel Scheduled Tasks

Daocloud + Docker + Laravel + crontab

The above successful attempt is to put these configuration into the Dockerfile, so that the container at the start of time to automatically open the Crontab Task Scheduler, so that all of this automatically to complete ~

Specific steps

1. Create the following crontab configuration file in the project./_linux/var/spool/cron/crontabs/root

* * * * * */usr/local/bin/php/app/artisan schedule:run >>/dev/null 2>&1

2. Copy the configuration file in Dockerfile to the directory where the configuration is crontab specified

From Php:7.0.7-apachemaintainer Jianyingli 
 
  
   
  # install cron command ... RUN apt-get update && apt-get install-y cron vim...# configuration crontab# Copy configuration file/var/spool/cron/crontabs/add _linux/var/s pool/cron/crontabs/root/var/spool/cron/crontabs/root# set the file owner and file Association group to Root:crontab, the affinity group must be crontabrun chown-r root: Crontab/var/spool/cron/crontabs/root \# Modify the permissions of the file must be 600, otherwise do not recognize && chmod 600/var/spool/cron/crontabs/root# create log The file run touch/var/log/cron.log...# the entrypoint.sh script with the command to start Apache and crontab, run chmod 777./entrypoint.shentrypoint [" ./entrypoint.sh "]
 
  

3. Add the./entrypoint.sh script and launch Apache and crontab inside

Php:7.0.7-apache This base image already contains a CMD [' apache2-foreground '] directive to start the Apache service, but we need to start Apache and crontab at the same time, So the script file is added and the relevant commands are added to it.

#!/bin/bashset-x# Save environment variables to/etc/default/localerm-rf/etc/default/localeenv >>/etc/default/locale# start crontab/ Etc/init.d/cron start# start apacheapache2-foregroundexec "$@"

Because of the execution mechanism of crontab, we cannot directly use environment variables configured through Daocloud background, but the configuration we apply is configured through environment variables, so we need to save these environment variables to/etc/default/locale through the env command. Crontab will load the environment variables in this file at startup, otherwise the PHP artisan schedule:run command will not be able to obtain the relevant application configuration, resulting in the inability to run the license we expected (for example, always unable to insert new data into the database)

Complete related documents

Here are three main configuration files that I used to build to run the Laravel5 application and should be able to meet most of the requirements.

. ├──_linux│   └──var│       └──spool│           └──cron│               └──crontabs│                   └──root├──dockerfile└──entrypoint.sh

./dockerfile

From Php:7.0.7-apachemaintainer Jianyingli
 
  
RUN apt-get update \ && apt-get install-y \ libmcrypt-dev \ libz-dev \ git \ Cron \ vim \ && docker-php-ext-install \ mcrypt \ mbstring \ pdo_my SQL \ zip \ && apt-get clean \ && apt-get autoclean \ && rm-rf/var/lib/apt/list s/*/tmp/*/var/tmp/*run Curl-ss Https://getcomposer.org/installer | PHP----install-dir=/usr/local/bin--filename=composeradd _linux/var/spool/cron/crontabs/root/var/spool/cron/ Crontabs/rootrun chown-r root:crontab/var/spool/cron/crontabs/root \ && chmod 600/var/spool/cron/crontabs/ Rootrun touch/var/log/cron.logrun a2enmod rewriteworkdir/appcopy./composer.json/app/copy./composer.lock/app/run Composer Install--no-autoloader--no-scriptscopy. /apprun rm-fr/var/www/html \ && ln-s/app/public/var/www/htmlrun chown-r www-data:www-data/app \ && Chmod-r 0777/app/storage \ && Composer Installrun chmod 777./entrypoint.shentrypoint ["./entrypoint.sh"] 
 

./_linux/var/spool/cron/crontabs/root

* * * * * */usr/local/bin/php/app/artisan schedule:run >>/dev/null 2>&1

./entrypoint.sh

#!/bin/bashset-xrm-rf/etc/default/localeenv >>/etc/default/locale/etc/init.d/cron Startapache2-foregroundexec "$@"

Example (the completion code can refer to the following project)

    • Phoenix-backend
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.