一名野生全栈工程师,喜欢研究各种新技术。
Follow Now最近项目涉及到一个定时任务的功能,所以去这几天研究了一下 crontab 的使用方法,按照网上的相关教程顺利在自己的电脑上成功开启了这个功能
Laravel + crontab
添加 crontab 配置
1、执行命令
$ crontab -e
2、添加以下内容( path/to为应用路径 ),即每分钟去执行一次以下的命令
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
3、启动(以下命令为Ubuntu下的启动命令,其他系统可能不一样)
$ /etc/init.d/cron start
完成以上三步即可以成功开启 Laravel 的任务调度功能了,下面验证一下
验证任务调度是否正常执行
./App\Console\Kernel
... protected function schedule(Schedule $schedule) { $schedule->call(function () { // 以下代码会插入一条 Tweet 数据 DB::table('tweets')->insert(['content' => 'Hi']); })->everyMinute(); }...
过几分钟后就可以去查看一下数据库有没有成功插入数据~
参考文章
- Laravel 任务调度
- docker下计划任务crontab的使用方法[python]
- Laravel 定时任务
DaoCloud + Docker + Laravel + crontab
上面的尝试成功后就是将这些配置到 Dockerfile 里,让容器在启动的时间自动开启 crontab 任务调度,让这一切都自动去完成~
具体步骤
1、在项目创建以下 crontab 配置文件 ./_linux/var/spool/cron/crontabs/root
* * * * * /usr/local/bin/php /app/artisan schedule:run >> /dev/null 2>&1
2、在 Dockerfile 里将配置文件复制到 crontab 指定的配置所在目录
FROM php:7.0.7-apacheMAINTAINER JianyingLi <lijy91@foxmail.com># 安装 cron 命令... RUN apt-get update && apt-get install -y cron vim...# 配置 crontab# 复制配置文件 /var/spool/cron/crontabs/ADD _linux/var/spool/cron/crontabs/root /var/spool/cron/crontabs/root# 设置文件所有者和文件关联组为 root:crontab ,关联组必须为 crontabRUN chown -R root:crontab /var/spool/cron/crontabs/root \# 修改文件的权限,必须为 600,否则不认 && chmod 600 /var/spool/cron/crontabs/root# 创建 log 文件RUN touch /var/log/cron.log...# 在 entrypoint.sh 脚本里加入启动 apache 和 crontab 的相关命令RUN chmod 777 ./entrypoint.shENTRYPOINT ["./entrypoint.sh"]
3、添加 ./entrypoint.sh 脚本,并在里面启动 apache 和 crontab
php:7.0.7-apache这个基础镜像已经包含了一个 CMD ['apache2-foreground']指令用于启动 apache 服务,但是我们需要同时启动apache 和 crontab ,所以增加了这个脚本文件并在里面加入了相关的命令。
#!/bin/bashset -x# 将环境变量保存至 /etc/default/localerm -rf /etc/default/localeenv >> /etc/default/locale# 启动 crontab/etc/init.d/cron start# 启动 apacheapache2-foregroundexec "$@"
由于crontab的执行机制,所以无法直接使用通过DaoCloud后台配置的环境变量,但是我们应用的配置都是通过环境变量来配置的,所以需要通过 env 命令将这些环境变量保存到 /etc/default/locale里,crontab 在启动时会加载这个文件里的环境变量,否则在执行 php artisan schedule:run命令会无法获取相关的应用配置,导致无法执照我们预想的去运行(例如始终无法插入新数据到数据库)
完整相关文件
以下是三个主要是配置文件,我是用来构建运行 Laravel5 应用的,应该可以满足大部分的需求。
.├── _linux│ └── var│ └── spool│ └── cron│ └── crontabs│ └── root├── Dockerfile└── entrypoint.sh
./Dockerfile
FROM php:7.0.7-apacheMAINTAINER JianyingLi <lijy91@foxmail.com>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 "$@"
示例(完成代码可以参考下面的项目)
- phoenix-backend

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software