【问题标题】:Passing parameter in crontab through shell to php script通过shell将crontab中的参数传递给php脚本
【发布时间】:2017-04-03 06:03:06
【问题描述】:

从我的 shell 脚本 test.sh 我想将一些参数传递给 PHP 脚本,它们会像这样读取它们:

test.sh

php -q /home/user/files/test.php "$1" "$2"
php -q /home/user/files/test.php $1 $2

为了测试参数的传递,我只是这样阅读它们(test.php):

<?php
   echo 'Arg 1: ' . $argv[1] ."\n";
   echo 'Arg 2: ' . $argv[2] ."\n";
?>

问题是,如果我使用命令从 shell 运行 test.sh:

./test.sh one two

我的 php 脚本读取两个参数都很好(两次):

Arg 1: one
Arg 2: two
Arg 1: one
Arg 2: two

但如果我通过 crontab 运行它,它无法读取参数,我只能得到:

Arg 1: 
Arg 2: 
Arg 1: 
Arg 2: 

Cron 作业如下所示:

20  13  *   *   *   /home/user/files/test.sh one two

如何通过 cron 作业正确地将参数传递给 shell 脚本,然后再传递给 php?

【问题讨论】:

    标签: php shell cron crontab cron-task


    【解决方案1】:

    您的 php.ini 中可能没有启用 register_argc_argv

    使用 php 的 getopt 获取命令行参数。

    http://php.net/manual/en/function.getopt.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多