-1

I have this line of code to execute a background task (convert several pngs to jpg)

exec("nohup path/to/php path/to/convertToJpg.php >> path/to/convert_to_jpg.log > /dev/null &");

Now I am writing the convertToJpg.php script and I can't figure out how to output information from there so that it will be logged into the convert_to_jpg.log.

When I try to google it all I come up with is how to call a php script from exec or shell_exec, since the words used to describe the two situations is almost the same.


For Clarification

An extra quote got into my code when copying it over to SO. I have fixed it. In my original code the convertToJpg.php is being called as expected, confirmed by error_logs placed in it to check.

A couple answers have pointed to the $output argument in exec(). As I understand it, that totally defeats the purpose of shell redirection using the >> path/to/convert_to_jpg.log.

My question is not how to get the output from the exec() command, but what code do I use to actually output(verb) from the convert_to_jpg.log


More Clarification

If I call

exec("nohup path/to/php path/to/convertToJpg.php >> path/to/convert_to_jpg.log > /dev/null &");

or

$results = shell_exec("path/to/php path/to/convertToJpg.php > /dev/null");
echo $results;

or

$results = "";
exec("path/to/php path/to/convertToJpg.php > /dev/null", $results);
print_r($results);

It doesn't matter which one.

Here is convertToJpg.php

<?php
    echo "Will this be in $results?"; // No, this did not end up in results.

    error_log("error logs are being logged, so I know that this php file is being called");

    //I have tested echo, but that does not work.  
    //What php function do I use so that a string will be captured in the $output of an exec() call?
?>
Joshua Goossen
  • 1,714
  • 1
  • 17
  • 36

2 Answers2

0
$output = "";
$return_var = "";
exec("nohup path/to/php path/to/convertToJpg.php >> path/to/convert_to_jpg.log > /dev/null &", $output, $return_var);

$output

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

$return_var

If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

http://php.net/manual/en/function.exec.php

Kivylius
  • 6,357
  • 11
  • 44
  • 71
  • Isn't that the point of shell redirection? That's what the '>>>> path/to/convert_to_jpg.log' is for. What I'm asking is what code do I use in convertToJpg.php to 'echo' to the output. – Joshua Goossen Jan 14 '16 at 15:22
  • First of all, its `$output`, this will print everything you "echo" out from the script "convert_to_jpg" , line by line in an array. The >>> is to write to the file, echo is separate, but your question is formatted wrong. – Kivylius Jan 14 '16 at 15:27
  • But `$output` won't work because I am not waiting for the command to finish. By the time that script finished the session will have been closed. Hence the `nohup[...]&` – Joshua Goossen Jan 14 '16 at 15:38
  • Then can't you just use `$file_string = file_get_contents(...)` on the `convert_to_jpg.log` file and import it into a string? http://php.net/manual/en/function.file-get-contents.php You say you want to output from a log file, its a non executive file, so you can't output from it, but you can print/parse it. – Kivylius Jan 14 '16 at 15:56
  • No, somehow, I'm having a hard time making myself clear. Maybe that's why I can't find the answer myself in the first place. I do not want to output from the log file. What code do I use to output from `convertToJpg.php` so that it would so that show in either the `$output` argument or in the `convert_to_jpg.log` file, depending on how it is implemented. – Joshua Goossen Jan 14 '16 at 16:10
0

Ok, I figured it out!

To answer the question plain and simply, use either echo or print.

This was the first thing I tried, but it didn't work, which made me think it was some other function that I was supposed to call. (I obviously haven't worked with this much)

The real problem I was having was the > /dev/null, which was discarding all output. Once I deleted that, it fine. Explained here: What is /dev/null 2>&1? I had at some point done a copy/paste without really understanding what that did...

And because I was blaming the wrong thing I ended up with this off base question.

Community
  • 1
  • 1
Joshua Goossen
  • 1,714
  • 1
  • 17
  • 36