项目环境:Linux+Nginx+php7.3+Mysql8.0 +(Laravel5.8框架)
问题:
1.php使用exec()调用计划任务,到时间了计划任务没有执行,应该从什么角度出发去排查问题。
2.使用root什么在命令行中执行 at -f /www/wwwroot/ipotest.liutong.pro/public/send.sh 1531 是可以执行的,任务身份为root
3.使用PHP exec() 执行上述命令 在命令行中使用 at -l 是可以打印出任务的,任务身份为www
4.我怀疑是身份的问题,但是不知道该从哪里入手开始解决,求指点
情景:现有一活动,需要在活动开始前1小时向关注此活动的用户发送优惠卷(这人计划任务要求在指定时间 且 仅执行一次)。
我的实现思路:
开始
↓
用户点击按钮关注活动,使用exec()指定linux命令发起计划任务。
↓
linux发起指定预制的.sh文件中的curl命令。
↓
linux发起curl请求指定路由,执行相关发送优惠卷逻辑。
↓
End;
我的实现代码如下:
1.发起计划任务 and 接收服务器计划任务相关业务逻辑
/**
* 设置Linux计划任务
*/
public function setScheduledTask(Request $request){
if(!$request['date']){
dd('请输入date,格式:时分,如:今日15时18分,则输入1518');
}
//at -f /www/wwwroot/ipotest.liutong.pro/public/send.sh 1737 2>&1
$date = $request['date'];//执行时间今天的17时30分,若执行时间小于当前时间,则执行时间自动改为次日的17时30分
$command = "at -f /www/wwwroot/ipotest.liutong.pro/public/send.sh {$date} 2>&1";
exec($command,$output,$return_var);
$res = '------'. date('Y-m-d H:i:s') . '发起调度任务-----\t';
file_put_contents('a.text', $res);
dd($output,$return_var);
}
/**
* 接收Linux调度任务请求
* @param Request $request
*/
public function receiveDispatcherTask(Request $request){
$res = date('Y-m-d H:i:s') . '接收到调度任务';
file_put_contents('b.text', $res);
}
2.send.sh 文件内容
curl https://xcx.liutong.pro/test/receiveDispatcherTask