【问题标题】:run php code with javascript code using crontab in linux在 linux 中使用 crontab 运行 php 代码和 javascript 代码
【发布时间】:2020-02-17 17:54:34
【问题描述】:

我正在尝试将包含 JavaScript 代码的 php 代码使用 phantomjs 和 firefox 浏览器放入 crontab。但它不起作用。如果我使用 CURL 运行,那么它会运行但 js 代码不起作用。下面是我的 crontab 命令。

* * * * * /usr/local/bin/phantomjs http://XX.XX.XXX.XXX/project/index.php > /dev/null 2>&1

* * * * * /usr/bin/firefox http://XX.XX.XXX.XXX/project/index.php > /dev/null 2>&1

有人告诉我可以在 crontab 中运行 php 和 js 代码吗?如果可以,那么有什么更简单的方法?

【问题讨论】:

    标签: php linux cron phantomjs


    【解决方案1】:

    作为PhantomJS,这是一个基于 JavaScript 的自动化框架,可用于 Windows、Mac OS X、Linux 和其他 *ix 系统。

    使用 PhantomJS,您可以执行以下操作:

        console.log('Loading a web page');
    
        var page = new WebPage();
        var url = "http://www.phantomjs.org/";
    
        page.open(url, function (status) {
            // perform your task once the page is ready ...
            phantom.exit();
        });
    

    或评估一个页面的标题:

        var page = require('webpage').create();
        page.open(url, function (status) {
            var title = page.evaluate(function () {
                return document.title;
            });
            console.log('Page title is ' + title);
        });
    
    

    来自PhantomJS' Quickstart 页面的示例。您甚至可以使用render() 方法将页面呈现为 PNG、JPEG 或 PDF。

    所以在你的 crontab 中

    * * * * * /usr/local/bin/phantomjs /home/user/custom.js > /dev/null 2>&1
    
    

    【讨论】:

    • 所以这意味着我无法使用 PhantomJS 运行 php 代码
    • 仅 PHP 代码使用 * * * * * curl -0 http://XX.XX.XXX.XXX/project/index.php > /dev/null 2>&1 `
    • 但这将在没有 JS 事件的情况下执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2017-01-16
    • 2019-03-18
    • 2011-07-15
    • 2019-08-31
    相关资源
    最近更新 更多