try .. catch with system()命令php on laravel 5.0 [英] try .. catch with system() command php on laravel 5.0

查看:190
本文介绍了try .. catch with system()命令php on laravel 5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码为命令应用程序在laravel 5.0与try ..抓住并使用系统命令。当有异常时,我的抓取不起作用。

 使用Illuminate\Support\Facades\Log; 
...
try {
system('。dump master | sqlite3'。$ older。'>'。storage_path()。'/ tmp /'。$ tabla。 sql');
} catch(\Exception $ e){
Log :: alert('问题导入旧表'$ tabla。$ e-> GetMessage());
}

我在shell上看到我的错误,但不能在我自己的laravel日志上写。 (Log:alert)

解决方案

您必须通过 STDERR 并且可能是 STDOUT 。使用 proc_open ,例如:

  $ desc = [
1 => ['pipe','w'],// STDOUT
2 => ['pipe','w'],// STDERR
];

$ proc = proc_open('ls -l。something',$ desc,$ pipes);
if(is_resource($ proc)){

if($ out = stream_get_contents($ pipes [1])){
echo $ out;
}
fclose($ pipes [1]);


if($ err = stream_get_contents($ pipes [2])){
fprintf(STDERR,Error:%s\\\
,$ err);
}
fclose($ pipes [2]);

//您还可以检查进程退出状态
// 0表示成功,否则为错误。
$ exit_status = proc_close($ proc);
}

当然,在 STDOUT pipe,如果命令重定向到一个文件。



是的, system()不会抛出异常。显然,您可以实现自己的类,如果进程退出状态不为零,或者 STDERR pipe中存在某些东西,则会抛出异常:

  class MyShellException extends \Exception {} 

class MyShell {
public static function execute($ command,& $ out = null){
if(func_num_args()> 1){
$ desc [1] = ['pipe','w' ]。
} else {
$ desc [1] = ['file','/ dev / null'];
}

$ desc [2] = ['pipe','w'];

$ proc = proc_open($ command,$ desc,$ pipes);
if(is_resource($ proc)){
if(isset($ pipes [1])){
$ out = stream_get_contents($ pipes [1]);
fclose($ pipes [1]);
}

if($ err = stream_get_contents($ pipes [2])){
fclose($ pipes [2]);
throw new MyShellException(Command $ command failed:$ err);
}

if($ exit_status = proc_close($ proc)){
throw new MyShellException(Command $ command exited with non-zero status);
}
}
}
}


try {
MyShell :: execute('ls -l。something' ,$ out);
echoOutput:$ out\\\
;
} catch(MyShellException $ e){
if(!empty($ out)){
echoOutput:$ out\\\
;
}
fprintf(STDERR,MyShell error:。$ e-> getMessage());
exit(1);
}


I've a code for command app made on laravel 5.0 with try .. catch and use system command. When there a exception, my catch not work.

use Illuminate\Support\Facades\Log;
...
try {
  system('.dump master | sqlite3 ' . $older . ' > ' . storage_path() . '/tmp/' . $tabla . '.sql' );
} catch ( \Exception $e) {
  Log::alert('Problem import old table '. $tabla . $e->GetMessage());
}

I see my error on shell but not write on my own laravel log. (Log:alert)

解决方案

You have to gain access over STDERR and, probably, STDOUT. Use proc_open, e.g.:

$desc = [
  1 => ['pipe', 'w'], // STDOUT
  2 => ['pipe', 'w'], // STDERR
];

$proc = proc_open('ls -l . something', $desc, $pipes);
if (is_resource($proc)) {

  if ($out = stream_get_contents($pipes[1])) {
    echo $out;
  }
  fclose($pipes[1]);


  if ($err = stream_get_contents($pipes[2])) {
    fprintf(STDERR, "Error: %s\n", $err);
  }
  fclose($pipes[2]);

  // You can also check the process exit status
  // 0 means success, otherwise error.
  $exit_status = proc_close($proc);
}

Of course, there is no need in STDOUT pipe, if the command redirects it to a file.

And yes, system() won't throw exceptions. Obviously, you can implement your own class which will throw an exception in case if the process exit status is non-zero, or there is something caught in the STDERR pipe:

class MyShellException extends \Exception {}

class MyShell {
  public static function execute($command, &$out = null) {
    if (func_num_args() > 1) {
      $desc[1] = ['pipe', 'w'];
    } else {
      $desc[1] = ['file', '/dev/null'];
    }

    $desc[2] = ['pipe', 'w'];

    $proc = proc_open($command, $desc, $pipes);
    if (is_resource($proc)) {
      if (isset($pipes[1])) {
        $out = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
      }

      if ($err = stream_get_contents($pipes[2])) {
        fclose($pipes[2]);
        throw new MyShellException("Command $command failed: $err");
      }

      if ($exit_status = proc_close($proc)) {
        throw new MyShellException("Command $command exited with non-zero status");
      }
    }
  }
}


try {
  MyShell::execute('ls -l . something', $out);
  echo "Output: $out\n";
} catch (MyShellException $e) {
  if (!empty($out)) {
    echo "Output: $out\n";
  }
  fprintf(STDERR, "MyShell error: " . $e->getMessage());
  exit(1);
}

这篇关于try .. catch with system()命令php on laravel 5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆