简体   繁体   English

Cron 作业中的 PHP 不运行简单的 Javascript 代码 :(

[英]PHP from Cron job doesn't run a simple Javascript code :(

I'm running an API to retrieve information.我正在运行一个 API 来检索信息。 I have to call this API via php 5 times, but each time I have to wait 60 seconds.我必须通过 php 调用这个 API 5 次,但每次我都必须等待 60 秒。 So the PHP File is running for like 6 minutes and gets timed-out.所以 PHP 文件运行了大约 6 分钟并超时。 I tried extending the time limit but that doesn't work so I thought of another solution.我尝试延长时间限制,但这不起作用,所以我想到了另一种解决方案。

Since I have to run this PHP anyway on CRON job, here is the setup:由于无论如何我都必须在 CRON 作业上运行此 PHP,因此设置如下:

-- A.php is run every 10 minutes scheduled in Cron manager. This now runs the header("B.PHP?round=1") command and loads B.PHP

----   B.PHP runs, does what it needs to, now uses javascript setInterval waits 60 seconds and loads (window.location.href ="B.PHP?round=2" again with new parameter (to run 2nd or 3rd etc api token).

THE problem is, it never does load the B.PHP again for second round.问题是,它永远不会在第二轮再次加载 B.PHP。 I tried doing ajax query xmlhttp all type of JS script to load a page.....NOTHING!我尝试做 ajax 查询 xmlhttp 所有类型的 JS 脚本来加载页面.....没有! It seems to either ignore the javascript completely, or just ignores applying the JS code that loads b.php with new parameter它似乎要么完全忽略了 javascript,要么只是忽略了使用新参数加载 b.php 的 JS 代码


I really don't want to use the sleep(60) method (well it times out anyway).我真的不想使用 sleep(60) 方法(无论如何它都会超时)。 and I have to use Cron job and I know javascript is the only way to make the script just chill during a wait without causing timedout.而且我必须使用 Cron 作业,我知道 javascript 是使脚本在等待期间冷却而不导致超时的唯一方法。 Any solutions at all?有什么解决办法吗? Please guys..be gentle I'm a biiit new at this stuff and know nothing about linux/ubunto :(拜托各位..温柔点我是这个东西的新手,对 linux/ubunto 一无所知 :(

ps: The B.php I have the entire URL still doesn't work. ps:我有完整网址的B.php 还是不行。 I HAVE To call a PHP file from the cron manager.我必须从 cron 管理器调用一个 PHP 文件。

I KNOW javascript is only on client side, but, the JS code is...loading a file on the server .. ?我知道 javascript 只在客户端,但是,JS 代码是......在服务器上加载文件......? Ugh...I don't know what to do :/呃......我不知道该怎么办:/

As you said correctly, JavaScript is only client side.正如您所说的那样,JavaScript 只是客户端。

Also, cron jobs usually only request a given URL but do not do anything with that result.此外,cron 作业通常只请求给定的 URL,但不会对该结果执行任何操作。 And they obviously do not execute javascript.而且他们显然不执行javascript。

You need to place the whole logic into your PHP code and use cronjobs to "trigger" your script(s).您需要将整个逻辑放入 PHP 代码并使用 cronjobs 来“触发”您的脚本。

  • Cronjob 1: Running every 10 minutes: start.php Cronjob 1:每 10 分钟运行一次: start.php
  • Cronjob 2: Running every 60 Seconds (maybe make it a little more, if your API has a limit of exactly 60 seconds): process.php Cronjob 2:每 60 秒运行一次(如果您的 API 的限制正好是 60 秒,则可能会多一点): process.php

Since you are using only PHP, you need to store your variable somewhere on your server.由于您只使用 PHP,因此您需要将变量存储在服务器上的某个位置。 This could either be a database or a file on your filesystem.这可以是数据库或文件系统上的文件。 You can find a more detailed explanation on how to persist a variable here: PHP store a single variable on the server?您可以在此处找到有关如何持久保存变量的更详细说明: PHP store a single variable on the server? . . (In my example I use a file as storage) (在我的示例中,我使用文件作为存储)

prosess.php:进程.php:

// number of times the script should be executed
$maxRounds = 5;

// load $round from your storage
$round = file_get_contents('store.txt');

if ($round < $maxRounds) {
    // increase round number for the next call
    // you may want to add some checks to determine if the current round was successful before increasing the value
    // depending on how log your round takes, it might be wise to add another variable (eg "working") to the store, so that multiple calls to the process file do not overlap
    file_put_contents('store.txt', $round + 1);

    // execute your code using the $round argument
    doRound($round);
}

else {
    // already done all rounds
}

start.php启动文件

// reset the $round variable
file_put_contents('store.txt', 0);

Keep in mind that this code is not production ready, but it should point you in the right direction :)请记住,此代码尚未准备好用于生产,但它应该为您指明正确的方向:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM