Linux 将 $_GET 参数传递给 cron 作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11297945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:12:44  来源:igfitidea点击:

Passing $_GET parameters to cron job

phplinuxgetcron

提问by Dfranc3373

I am new at cron jobs and am not sure whether this would would work.

我是 cron 工作的新手,不确定这是否可行。

For security purposes I thought about making a one page script that looks for certain GET values (a username, a password, and a security code) to make sure that only the computer and someone that knows all 3 can run the command.

出于安全目的,我考虑制作一个单页脚本来查找某些 GET 值(用户名、密码和安全代码),以确保只有计算机和了解所有 3 个值的人才能运行该命令。

I made the script and it works running it in a browser but is it possible to run the cron job with GET values?

我制作了脚本并且它可以在浏览器中运行它,但是是否可以使用 GET 值运行 cron 作业?

a example would be me running

一个例子是我跑步

* 3 * * * /path_to_script/cronjob.php?username=test&password=test&code=1234

Is this possible?

这可能吗?

采纳答案by RobB

The $_GET[]& $_POST[]associative arrays are only initialized when your script is invoked via a web server. When invoked via the command line, parameters are passed in the $argvarray, just like C.

$_GET[]$_POST[]当你的脚本通过Web服务器调用关联数组对象仅被初始化。当通过命令行调用时,参数在$argv数组中传递,就像 C 一样。

Contains an array of all the arguments passed to the script when running from the command line.

包含从命令行运行时传递给脚本的所有参数的数组。

Your command would be:

你的命令是:

* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234 

You would then use parse_str()to set and access the paramaters:

然后,您将使用parse_str()来设置和访问参数:

<?php

var_dump($argv);

/*
array(4) {
  [0]=>
  string(27) "/path_to_script/cronjob.php"
  [1]=>
  string(13) "username=test"
  [2]=>
  string(13) "password=test"
  [3]=>
  string(9) "code=1234"
}
*/

parse_str($argv[3], $params);

echo $params['code']; // 1234

回答by jeroen

Not a direct answer to your question but a better solution I think:

不是对您问题的直接回答,而是我认为更好的解决方案:

If you want nobody except cron to run the script, just place it outside the web-root. That way there is no access via the web-server at all.

如果你希望除了 cron 之外没有人运行脚本,只需将它放在 web-root 之外。这样就根本无法通过网络服务器进行访问。

If you do need to run the command as a special user as well, don't use GETbut have a user login and check for a logged-in session (a certain set session variable...) and include the script in that page only.

如果您确实需要以特殊用户身份运行命令,请不要使用GET但有用户登录并检查登录会话(某个设置的会话变量...)并仅在该页面中包含脚本.

Your publicly accessible script would look something like:

您可公开访问的脚本如下所示:

session_start();

if (isset($_SESSION['user']))
{
  include '/path/to/script/outside/of/web-root';
}
else
{
  die('No access.');
}

回答by ZnArK

This will work:

这将起作用:

* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234

回答by Mike Mackintosh

You should look into the get_opt()or $argvfunction.

您应该查看get_opt()$argv函数。

回答by Ambarish

* 3 * * * /path_to_script/cronjob.php?username=test&password=test&code=1234

This will not work

这行不通

* 3 * * * /path_to_script/cronjob.php username=test password=test code=1234

This works

这有效

Also, you need to use parse_str() function and get value

此外,您需要使用 parse_str() 函数并获取值

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

回答by golabs

I realize that this post is already several years old but this one helped me the most so let me share my findings as well to help others too.

我意识到这篇文章已经有好几年了,但这篇文章对我帮助最大,所以让我也分享我的发现以帮助其他人。

I'm using DirectAdmin and my cron examples are working both. I removed my email address from the 'Send all Cron output to E-Mail' so I don't receive email notifications of my cronjobs.

我正在使用 DirectAdmin 并且我的 cron 示例都可以使用。我从“将所有 Cron 输出发送到电子邮件”中删除了我的电子邮件地址,因此我没有收到有关我的 cronjob 的电子邮件通知。

I started off with a cURL request which runs every 20 minutes:

我从每 20 分钟运行一次的 cURL 请求开始:

*/20 * * * * /usr/local/bin/curl --silent 'https://demo.tld/app/stats/?update&key=1234'

Please note the ' ' around the URL, otherwise you can't add multiple parameters!

请注意网址周围的' ',否则无法添加多个参数!

I use that page as an API inspired way to trigger the update of some statistics I collect from another website and which others can grab in JSON form from mine. The parameter 'update' triggers the update process and the parameter 'key' will, when validated, trigger additional update actions I only want to be done when the cronjob requests the update.

我使用该页面作为 API 启发的方式来触发更新我从另一个网站收集的一些统计数据,而其他人可以从我的 JSON 格式中获取这些数据。参数 'update' 触发更新过程,参数 'key' 将在验证后触发我只想在 cronjob 请求更新时执行的其他更新操作。

Since above cronjob basically consumes bandwidth in both directions I wanted to go for a PHP based cronjob, but ran into a problem with the parameters... so that is when I found this post which saved my day :)

由于上面的 cronjob 基本上在两个方向上都消耗带宽,我想使用基于 PHP 的 cronjob,但是遇到了参数问题......所以我发现这篇文章拯救了我的一天:)

*/20 * * * * /usr/local/bin/php /home/path/to/public_html/app/stats/index.php update key=1234

As you can see the filename (index.php) is now included in the path and then the parameters follow (without the ? and &'s).

如您所见,文件名 (index.php) 现在包含在路径中,然后是参数(没有 ? 和 & )。

This way you get the cronjob working BUT you're only half way since the parameters won't be passed on via $_GET... which is a bit annoying when you've coded your script with checks for $_GETkeys!

通过这种方式,您可以使 cronjob 正常工作,但是您只完成了一半,因为参数不会通过$_GET...传递……当您使用检查$_GET键对脚本进行编码时,这有点烦人!

So, how does it work then? Simple (at least after some research), the cronjob passes the parameters to the script via a variable named $argv.

那么,它是如何工作的呢?简单(至少经过一些研究),cronjob 通过名为$argv.

So with that knowledge I searched for a method to transform the $argvinto $_GETso:

因此,凭借这些知识,我搜索了一种将 转换$argv$_GET这样的方法:

  1. I can trigger the update both manually and via the cronjob.
  2. I didn't had to rewrite my whole script.
  1. 我可以手动和通过 cronjob 触发更新。
  2. 我不必重写我的整个脚本。

I found the following solution which we only want to execute when $argvis actually set, so I wrapped it in the if issetcheck:

我找到了以下我们只想在$argv实际设置时执行的解决方案,所以我将它包装在if isset检查中:

if( isset( $argv ) )
{
    foreach( $argv as $arg ) {
        $e = explode( '=', $arg );
        if( count($e) == 2 )
            $_GET[$e[0]] = $e[1];
        else    
            $_GET[$e[0]] = 0;
    }
}

Hope this helps you too :)

希望这对你也有帮助:)

回答by Peter

I find it usefull to use the getopt()function when I need parameters from a cron or a console command.

getopt()当我需要来自 cron 或控制台命令的参数时,我发现使用该函数很有用。

This function will return an array of option / argument pairs, or FALSE on failure.

此函数将返回一组选项/参数对,或失败时返回 FALSE。

The following examples have been directly taken from the PHP documentation.

以下示例直接取自 PHP 文档。

$options = getopt("f:hp:");
var_dump($options);


shell> php example.php -fvalue -h

The above example will output:

上面的例子将输出:

array(2) {
    ["f"]=>
    string(5) "value"
    ["h"]=>
    bool(false)
}

For more options and different parameters take a look at the full documentation.

有关更多选项和不同参数,请查看完整文档。

Resources

资源

回答by Marco Marsala

username=test&password=test&code=1234

username=test&password=test&code=1234

will became:

将变成:

php -r "$_GET["username"]="test"; $_GET["password"]="test"; $_GET["code"]="1234"; include "wp-cron.php";'

php -r "$_GET["username"]="test"; $_GET["password"]="test"; $_GET["code"]="1234"; include "wp-cron.php";'