【问题标题】:how to pass arguments to linux at command如何在命令中将参数传递给linux
【发布时间】:2013-11-27 14:37:56
【问题描述】:

我需要稍后运行 mail.php 文件,而不是让用户在提交 register.php 时等待发送验证电子邮件。

所以我选择在 1 分钟后在命令行中使用 at 命令运行 mail.php(invoked in register.php):

但我只能在 at 命令的交互模式下向该 php 文件发送参数。

at now + 1 minute
at> php mail.php {email}     # {email} is the argument I want to pass

因为我希望这是自动的,所以我需要使用 at 运行一个 shell 脚本:

at -f mail.sh

但我找不到传递 {email} 参数的正确方法,

我尝试在 Shell 中设置环境变量,但也是徒劳的:

register.php 文件中,我写道:

shell_exec('export email=foo@bar.com');
shell_exec('at -f mail.sh now + 1 minute');

ma​​il.sh 中,我写道:

#! /bin/bash
php mail.php $email

【问题讨论】:

  • 为什么 mail.php 阻塞到你担心让用户在执行时等待的程度?
  • 可能是由于我尚未调查的服务器上的某些路由问题或某些网络带宽。顺便说一句,我在mail.php中使用了PHPMailer。
  • IIRC PHPMailer 将邮件提交到 SMTP 服务器以进行排队和分派。所以它根本不应该阻塞。实现异步电子邮件发送比从 Web 应用程序开始使用系统 shell 要好得多,因为允许 shell 访问总是存在安全问题。
  • 知道如何实现异步电子邮件吗? @deed02392
  • 我只是说我认为这就是 phpmailer 的工作方式。我向我的本地 SMTP 服务器提交了一封电子邮件,该邮件需要 ms。您是否尝试直接连接到目标邮件服务器并以这种方式发送邮件,即在 PHP 中实现 SMTP 客户端?

标签: php linux shell


【解决方案1】:

你可以用这个:

shell_exec('echo php mail.php test@test.com | at now + 1 minute');

【讨论】:

    【解决方案2】:

    您可以从标准输入而不是从文件中读取命令。 (bash 的)here-doc 语法在这里很好用:

    shell_exec('at now + 1 minute <<EOF
        php mail.php test@test.com
    EOF
    ');
    

    【讨论】:

    • 附加说明:now + 1 minute 60 秒后不执行命令。它将在下一个 整分钟 执行命令,这意味着 HH:MM:00.. 如果您在 HH:MM:59 执行命令,这可能只是一秒钟的延迟
    • 奇怪,总是提示这个语法错误。最后看到的标记:+。乱码时间
    • 是的,它有效。我忘记了 here-doc 格式,以为它进入 bash 并且与 PHP 无关,所以我没有正确格式化 here-doc。谢谢!
    【解决方案3】:

    一口气:shell_exec('export email=foo@bar.com; at -f mail.sh now + 1 minute');

    或者只是:shell_exec('php mail.php foo@bar.com');

    【讨论】:

    • ...或者第二个cmd如何使用at来延迟cmd?
    【解决方案4】:

    1) 对于 Linux。检查文件

    /etc/at.deny

    删除用户“www-data”(如果存在)。

    2) 用 php 安排简单的命令。

    Linux

    exec('echo "mkdir /tmp/testAT" | at now + 1 minute');
    

    窗户

    exec('at "16:45:01 20.09.2017" cmd /c mkdir C:\Server\_debug\1\testAT');
    

    3) 调度 Symfony 框架命令。

    exec('echo "php /var/www/mysite.com/app/console system:test_command param1 param2 --env=test" | at 11:14 AM 19.09.17');
    

    【讨论】:

      【解决方案5】:

      我知道最初的问题已经得到了满意的回答,但是如果您在 PHP 脚本中使用 popen 来执行 at 命令,您可以像这样包含参数(作为 GET 参数):

      $target_script_path = "path/to/your/target/script";
      $time = "now";
      $file = popen("/usr/bin/at $time", "w");
      //$cmd = "/usr/bin/php $target_script_path"; // Working example with no arguments supplied.
      //$cmd = "/usr/bin/php $target_script_path?test_param=test_value"; // Trying to use a GET parameter like this does not work!
      $cmd = "/usr/bin/php $target_script_path test_param=test_value"; // Working example *with* argument!
      fwrite($file, $cmd);
      pclose($file);
      

      然后你可以在你的目标脚本中获取test_param 值:

      $test_param = $_GET['test_param']; // "test_value"
      

      【讨论】:

        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2010-09-11
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多