2

Is there a way to run a shell script from PHP and echo the results after progress is completed?

Here is my shell script:

(Its multilines - a few commands that have to be ran one after the other. )

cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save
npm start

here's my currently functioning PHP script. It only outputs some of the progress and only outputs it when complete. I need a live preview of the progress.

    <?php
echo "loaded<br><br>";
 // echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
1
  • Given that the script would be running npm * scripts, the user will likely need root/sudo access, right? Is that the same user that customers will use? If so, you might want to reconsider the approach to this deployment process. There are CI tools like jenkins that could be used to create a deployment script whenever you need to run those commands... Commented Mar 2, 2017 at 17:22

2 Answers 2

3

Here is another approach. It uses redirection to output the results and then file_get_contents() to read the output.

<?php

echo "loaded<br><br>";

$cmd = 'npm start';
$run = $cmd . ' > result.txt 2> errors.txt';
$output = shell_exec($run);

$results = file_get_contents('result.txt');
if (strlen(file_get_contents('errors.txt')) != 0) {
  $results .= file_get_contents('errors.txt');
}

echo "<pre>$results</pre>";

?>
Sign up to request clarification or add additional context in comments.

1 Comment

In order for @StarShows Studios to get this to work, he/she may need to put all the npm commands (e.g. npm uninstall gulp, npm install gulp, etc) into a shell script and run that script in $cmd in order to have the output get redirected to the txt files appropriately... also might need to set add the web user (www-data?) sudoers (see the Update to this question for more info)
0

I guess, ob_flush() would work:

    <?php
echo "loaded<br><br>";
ob_flush();

 // echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
ob_flush();
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

But I don't understand why you echo HTML-tags when you run that script on the console... Hope I got you right

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.