I'm trying to execute a linux command through a PHP command-line script, which is no problem using the exec command.
The problem is, the command I am executing (mysqldump) outputs an error message if something is wrong (for example user/password is incorrect). I can't seem to be able to capture this error in order to log it. It just prints this error to the screen.
How do I cause this error not to be printed to the screen, but instead to put it in a variable for use in my script?
Thanks!
Use popen to run the process. The example #2 on this page shows exactly what you're looking for:
tried using backticks?
Have you looked at the system() command? It's been a while since I did any PHP, but that rings a bell.
I'm not too hot in Unix (New Years Resolution...) but these functions look helpful:
shell_exec
- returns result as a string.passthru
- It looks like you can execute this like:passthru('command', $result);
and then use$result
.The following will route stderr messages to the same place as the normal output.
2>&1 means re-route stderr messages to the same place as stdout, and thus will be loaded into the output array.