doushenxu7294
2016-01-28 06:25
浏览 129
已采纳

如果由Cron运行,PHP`exec()`不起作用

I have a PHP script that is being called every minute by crontab like this:

* * * * * /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php

The script executes a number of other PHP scripts and passes a variable like so:

$ccmd = "nohup sudo /usr/bin/php watchdog.php " . $var . " &";
exec($ccmd);

When called from a browser, watchdogExec.php runs fine and does its job. However when I added the cronjob, the script is not working and is spamming mails with the following error:

No entry for terminal type "unknown";
using dumb terminal settings.

This error appears the same number of times exec() is called, so I figured that must be the issue. I searched and read about cronjob not running commands in a terminal, and I tried setting the environment in the crontask, but it did not help. Apparently PHP is printing that error, but I haven't found anything in their documentation about it.

* * * * *  /usr/bin/env TERM=xterm /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php

I am running CentOS 7.

Edit:

Searched some more and found out about PHP environment variables. Someone fixed a similar problem by adding the following in their PHP script. However this is not fixing my problem, it only stops the mail spam. The script is still not working.

putenv('SHELL=/bin/bash');
putenv('TERM=xterm');

图片转代码服务由CSDN问答提供 功能建议

我有一个PHP脚本,每分钟都被crontab调用,如下所示:

  * * * * * / usr / bin / php /var/www/html/cronjobs/watchdog/watchdogExec.php
nn

剧本 执行许多其他PHP脚本并传递一个如下变量:

  $ ccmd =“nohup sudo / usr / bin / php watchdog.php”。  $ var。  “&”; 
exec($ ccmd); 
   
 
 

从浏览器调用时, watchdogExec.php 运行正常并执行 工作。 但是,当我添加cronjob时,脚本无法正常工作并且发送垃圾邮件时出现以下错误:

 终端类型“未知”没有条目; 
使用哑终端设置。  
   
 
 

此错误与 exec()的调用次数相同,因此我认为这一定是个问题。 我搜索并阅读了cronjob没有在终端中运行命令,我尝试在crontask中设置环境,但它没有帮助。 显然PHP正在打印该错误,但我在他们的文档中没有找到任何关于它的内容。

  * * * * * / usr / bin / env TERM = xterm / usr  / bin / php /var/www/html/cronjobs/watchdog/watchdogExec.php
nn

我正在运行CentOS 7. < p>编辑:

搜索了一些有关PHP环境变量的信息。 有人通过在PHP脚本中添加以下内容来修复类似的问题。 但是,这不是解决我的问题,它只会阻止邮件垃圾邮件。 该脚本仍无法正常工作。

  putenv('SHELL = / bin / bash'); 
putenv('TERM = xterm'); 
  <  / pre> 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongzhou4727 2016-01-28 07:17
    最佳回答

    Have you tried using full paths in your exec statement too? Also try adding the TERM environment variable:

    $ccmd = "nohup /usr/bin/sudo /usr/bin/env TERM=xterm /usr/bin/php watchdog.php " . $var . " &";
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题