php:apache 基于docker cron的执行定时任务


前言

项目背景: 站点有SEO优化人员做siteMap,但是项目基于阿里云k8s集群,后端有多个容器,一时无法让所有容器同时生成sitemap,故希望采用sitemap版本机制。定时去判断版本,若有有地图更新,则重新生成sitemap.xml。其实此方案是不建议使用的,因为按照容器使用建议,一个容器就一个进程最佳。只是本着较真的劲儿,自己摸索了许久,终于找到解决方案。故放出予以记录,以后少踩点坑。

一、cron是什么?

计划任务,是任务在约定的时间执行已经计划好的工作,本案例是定时请求指定url,用于生成txt文本作为测试。

二、使用步骤

1.创建测试脚本

index.php:

<?php
//文件名称
$content=time();
$file = 'files/'.time().'.txt';
// echo($file);
// die;
$fp = fopen($file, 'a');
fwrite($fp, $content);
fclose($fp);
exit("over");

2.docker-compose文件

docker-compose.yml:

version: "3.8"
services:
  app_php:
    build:
      context: .
      dockerfile: Dockerfile_dev
    image: iws_pingtai #使用镜像
    ports: #指定端口
      - "9010:80"
    volumes: #挂载宿主机目录到容器目录
      - ".:/var/www/html" #挂载当前目录到容器目录
    networks:
      - net_pingtai
networks:
    net_pingtai:
        driver: overlay

3.Dockerfile_dev文件

Dockerfile_dev:

#引入基础镜像
FROM php:7.2.33-apache
#安装cron
RUN apt-get update && apt-get install -y cron vim

#复制cron配置文件到crontab中
COPY root /var/spool/cron/crontabs/root
#修改权限
RUN chown -R root:crontab /var/spool/cron/crontabs/root  \
&& chmod 600 /var/spool/cron/crontabs/root

#复制启动脚本
COPY startup.sh /startup.sh

#执行脚本
ENTRYPOINT ["/startup.sh"]

4.startup.sh文件

Dockerfile_dev:

#!/bin/sh
/etc/init.d/cron restart
apache2-foreground

5.cron配置文件

配置文件前面的备注说明不能删除,原因不清楚,开始我也认为备注说明都是一堆垃圾,影响阅读。只留下了业务配置,但是容器启动起来,cron能启动,但是就是不能执行作业。后测试在有备注的时候可以执行作业,此坑是测试最耗时的地方。还是经验不足啊。如下案例是1分钟请求一次http://127.0.0.1,用于执行index.php,生成测试文件。

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.JeaR4e/crontab installed on Sun Apr 18 07:02:47 2021)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
*/1 * * * * curl http://127.0.0.1

6.执行启动

命令行到项目目录下,运行docker-compose up -d,若无错误的话项目将会被成功启动起来,届时cront会执行root配置文档中定义好的作业,1分钟一次请求http://127.0.0.1,程序会在files目录下生成时间戳txt文件。

案例源码:https://download.csdn.net/download/txblovehq/16742545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值