search

Home  >  Q&A  >  body text

linux - php executes scheduled tasks?

用户添加定时任务后如
  每月30 号发工资
  订单到期未付款恢复
这种是如何做的?
直接在Linux 操作crontab 可以,
但是用户添加一个任务就加到 crontab 怎么实现?
巴扎黑巴扎黑2785 days ago1174

reply all(8)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-27 09:19:40

    Keywords: shell_exec, php safe mode

    shell_exec solves your problem of adding scheduled tasks. The shell_exec function cannot be used when PHP safe mode is turned on.

    reply
    0
  • 欧阳克

    欧阳克2017-06-27 09:19:40

    Scheduled tasks can be stored in MySQL or files, and then use crontab to run mysql or files

    reply
    0
  • 为情所困

    为情所困2017-06-27 09:19:40

    Use crontab, such as:

    0 0 30 * * 执行文件

    reply
    0
  • PHP中文网

    PHP中文网2017-06-27 09:19:40

    mysql stored procedures can also run scheduled tasks

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-27 09:19:40

    Use independent scheduled task middleware to manage, user scheduled tasks are not suitable to be placed in crontab

    reply
    0
  • 为情所困

    为情所困2017-06-27 09:19:40

    I'll quote Naist
    First of all, you need to be able to use crontab. Yes, if you don't know how to use it, just use Baidu. I won't answer it here. You can write a blog.
    Then in the command line

    crontab -e

    After

    0 0 30 * * php 需要定期执行的php脚本位置

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-27 09:19:40

    It may not be crontab. I used node.js to write a WebSocket that users can schedule by themselves before and hang in the background. When the user sends a request to this WebSocket, the scheduled time is recorded in a cache file (JSON), and then node.js sets its own timer.

    If the WebSocket service hangs up, just restart the background script, and the script will reset the scheduled task based on the time recorded in the cached JSON. I can put some code here, the core code is omitted, and some comments are added for reference

    'use strict';
    // 一些NodeJS包
    const WebSocket = require('ws');
    const path = require('path');
    const crontab = require('node-crontab');
    const spawn = require('cross-spawn');
    const fsExtra = require('fs-extra');
    const objectValues = require('object-values');
    
    // 省略了一些配置
    
    const wss = new WebSocket.Server({port: 8080}); // 创建WebSocket服务
    
    let ScheduleList = {}; // 这个为存储时间的JS对象
    let JobList      = {}; // 这个为存储定时任务ID的JS对象,如果要取消任务,就通过这个来查找定时任务ID
    
    // 省略了日志方法
    
    // 故障后重启载入任务时间的方法
    const loadJobs = () => {
        let jobsCache = path.resolve(__dirname, './config/scheduler.jobs');
    
        if(fsExtra.pathExistsSync(jobsCache)) {
            ScheduleList = fsExtra.readJsonSync(jobsCache, {throw: false}) || {};
        }
    };
    // 用户传入时间时将任务时间对象ScheduleList重写到文件的方法
    const saveJobs = () => {
        let jobsCache = path.resolve(__dirname, './config/scheduler.jobs');
        fsExtra.outputJsonSync(jobsCache, ScheduleList);
    };
    // 根据ScheduleList重新创建计划任务的方法
    const rerunJobs = () => {
        for(let jobScheduleId in ScheduleList) {
            let hour   = ScheduleList[jobScheduleId].hour || 0;
            let minute = ScheduleList[jobScheduleId].minute || 0;
    
            let jobId = crontab.scheduleJob(`${minute} ${hour} * * *`, () => {
                // 此处省略了任务创建的具体操作,用的cross-spawn包
            });
    
            JobList[id] = jobId;
        }
    };
    
    loadJobs(); // 启动时载入时间
    rerunJobs(); // 根据时间重新创建计划任务
    
    wss.on('connection', ws => {
        ws.on('message', message => {
            // 省略了传入数据的解析
    
            // 如果任务存在,先删除任务
            if(JobList[id]) {
                crontab.cancelJob(JobList[id]);
                delete ScheduleList[id];
                delete JobList[id];
            }
    
            // 重新添加任务
            let jobId = crontab.scheduleJob(`${minute} ${hour} * * *`, () => {
                // 同rerunJobs()里的创建操作,省略细节
            });
    
            ScheduleList[id] = {id: id, hour: hour, minute: minute};
            JobList[id]      = jobId;
    
            saveJobs(); // 保存任务计划时间
    
            ws.send(JSON.stringify(response(null, {message: '应用成功'}))); // 给请求者反馈
        });
    });

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-27 09:19:40

    For PHP, you can only use crontab to select scheduled tasks, so the shortest training rotation time is 1 second
    If you want it within 1 second, you have to find another way.

    reply
    0
  • Cancelreply