Recently, the project involves the function of a scheduled task, so I have studied the usage of crontab in recent days, and successfully opened this function on my computer according to the related online tutorials
Laravel + crontab
Add crontab configuration
1. Execute command
$ crontab -e
2. Add the following (path/to is the application path), i.e. execute the following commands every minute
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
3. Startup (the following command is the startup command under Ubuntu, which may be different from other systems)
$ /etc/init.d/cron start
After completing the above three steps, you can successfully start the task scheduling function of Laravel. Let's verify
Verify whether the task schedule is executed normally
./App\Console\Kernel
In a few minutes, you can check whether the database has successfully inserted data~
DaoCloud + Docker + Laravel + crontab
After the success of the above attempt, you can configure these to the Dockerfile to enable the container to automatically start crontab task scheduling at the start time, so that all of these can be done automatically~
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 to the directory specified by crontab in the Dockerfile
Must be crontabRUN chown -R root:crontab /var/spool/cron/crontabs/root \ ා the permission to modify the file must be 600, otherwise & & Chmod 600 / var / spool / cron / crontabs / root ා create the log file RUN touch /var/log/cron.log... # add the related command run Chmod 777. / entrypoint. Shantrypoint [". / entrypoint.sh"]
3. Add. / entrypoint.sh script, and start apache and crontab in it
php:7.0.7-apache this basic image already contains a CMD ['apache2-foreground '] instruction to start the Apache service, but we need to start Apache and crontab at the same time, so we added this script file and added related commands in it.
#!/bin/bashset -x# Save environment variables to /etc/default/localerm -rf /etc/default/localeenv >> /etc/default/locale# start-up crontab/etc/init.d/cron start# start-up apacheapache2-foregroundexec "$@"
Because of the execution mechanism of crontab, we can't directly use the environment variables configured through the background of DaoCloud, but the configuration of our application is configured through the environment variables, so we need to save these environment variables to / etc/default/locale through the env command, and crontab will load the environment variables in this file when it starts, otherwise, it will execute the PHP artican schedule: The run command will fail to obtain the relevant application configuration, resulting in the failure to license us to run as expected (for example, new data can never be inserted into the database)
Complete related documents
Here are three main configuration files. I am used to build and run larave5 applications, which should 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_mysql \ zip \ && apt-get clean \ && apt-get autoclean \ && rm -rf /var/lib/apt/lists/* /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 "$@"
I hope the above will help you
Please pay attention to my column for more PHP related knowledge PHPzhuanlan.zhihu.com