php - php 使用 at 命令执行 bash 脚本
问题描述
您好,我正在尝试在我的 php 脚本中执行 shell 命令,但它不起作用。我的 php 脚本:
//I save the Order
$holdedOrder->save();
$id = $holdedOrder->id;
$old_path = getcwd();
chdir(__DIR__.'/../');
$scriptFile = 'anacron_job_unhold_order.sh';
$bool = file_exists($scriptFile);
//$bool is true !
//this command works in shell but not in here do not know why
$s = shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when");
chdir($old_path);
return [$s,$bool];
$when 具有有效值4 hours
或4 days
...命令将是:
echo bash anacron_job_unhold_order.sh 29 | at now +1 minutes
输出为空。尝试exec()
返回 127 代码
编辑:我从 /etc/at.deny 中删除了 www-data 仍然是同样的问题
解决方案
该命令shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when");
可能是导致问题的原因,您应该仔细查看其工作原理at
。
该命令应该类似于
at [options] [job...]
at
要从命令而不是 STDIN分配作业,请使用heredoc syntax
at now + 1 minute <<< 'touch /tmp/test'
所以 PHP 代码应该是这样的;
$s = shell_exec("/usr/bin/at now + {$when} <<< '/usr/bin/bash {$scriptFile} {$id}'");
exec($s, $output, $return_var);
推荐阅读
- python - 在 Ubuntu 18.04 中使 python 模块适用于不同的用户
- bash - 每当出现新目录时如何在 BASH 中实现等待功能
- sql-server - 如何将文本列转换为 MS SQL Express 中的整数列?
- python - 如何将 Python Lambda 函数集成到 AWS Amplify 的管道中
- swift - 如何获取方法的定义类的名称?
- docker - 如何在不丢失容器数据的情况下从 docker 命名卷切换到基于路径的卷?
- vue.js - 过滤数组并仅显示过滤的项目
- google-apps-script - 缩短 Google 表格中录制的宏快捷方式
- java - 尝试读取 .crt 文件时权限被拒绝 - Android Studio Java
- timer - AHK 如何使一个 SetTimer 启动其他而不是反转