Sunday, February 22, 2015

PHP get exit code status of a command execution

Solution 1:

<?php
$cmd = 'ls -lpas';

# Execute the shell command
$shellOutput = shell_exec($cmd . ' > /dev/null; echo $?');

# Return execute status;
echo trim($shellOutput); 
?>

Solution 2:

$cmd = 'echo hi';
my_runShellCmd($cmd);

function my_runShellCmd($cmd) {
  return trim(shell_exec($cmd . ' > /dev/null; echo $?')) === '0' ? TRUE : FALSE;
}

No comments: