通过 crontab 一个变量并从 PHP 读取它?

2022-01-03 00:00:00 cron php

我创建了一个 crontab 规则:

I have created a crontab rule:

* * * * * php/my/directory/file.php

我想从这个 crontab 传递一个要在 file.php 中使用的变量.

I want to pass a variable to be used in the file.php from this crontab.

这样做的最佳方法是什么?

What is the best method to do this?

推荐答案

请记住,在 shell 中运行 PHP 与在 Web 服务器环境中运行完全不同.如果你之前没有做过命令行编程,你可能会遇到一些惊喜.

Bear in mind that running PHP from the shell is completely different from running it in a web server environment. If you haven't done command-line programming before, you may run into some surprises.

也就是说,将信息传递到命令中的常用方法是将其放在命令行中.如果你这样做:

That said, the usual way to pass information into a command is by putting it on the command line. If you do this:

 php /my/directory/file.php "some value" "some other value"

然后在你的脚本中,$argv[1] 将被设置为 "some value"$argv[2] 将被设置为设置为 其他值".($argv[0] 将被设置为 "/my/directory/file.php").

Then inside your script, $argv[1] will be set to "some value" and $argv[2] will be set to "some other value". ($argv[0] will be set to "/my/directory/file.php").

相关文章