I m a begineer in php and having some problem as :
I have a php file having a function create_dump() in which I am dumping my postgresql database using pg_dump command. Now while executing php file from linux terminal it asks for postgresql password on terminal. I want to add error handling in my php file if user provides wrong password for the database unintentionally.
In other words :
How to catch pg_dump (postgresql command) execution error from my php file ?
Thanks !
Based on the info you provided I have a workaround for what you want to do. First, instead of piping the output of your dump using the '>' you will need to use the --file=some_file.sql flag instead which does the same job.
Now, it does not appear that the $output from the passthru call will capture the output even if you send stderr to stdout so here's what would work. You would modify the command using the --file= flag and pipe the output of the command (which contains the error) to a log file while making sure to also send stderr to stdout (do 2>&1 after the command) in order to capture the error.
Here's the code:
// notice the file= flag and the piping of error to the file
$command=
"/usr/bin/pg_dump --a --no-owner --no-acl --attribute-inserts ".
"--disable-dollar-quoting --no-tablespaces ".
"--file={$path['schema_path']}schema-{$latest_update}.sql ".
"--host={$db['hostname']} --user={$db['username']} ".
"--password --port={$db['port']} {$db['database']} > pg_dump_error.log 2>&1";
// no output to capture so no need to store it
passthru($command);
// read the file, if empty all's well
$error = file_get_contents('pg_dump_error.log');
if(trim($error) != '')
exit("PG_DUMP ERRROR:\n-----\n$error\n");
Hope this is more or less what you were looking for.
Related
I'm developing a code which uses ldap_search Shell Script Command for extracting user information from Active Directory using user id and by proper LDAP Server Authentication. I am getting accurate result from ldap_search script.
But, whenever I put the shell script inside exec or shell_exec PHP command, I'm not getting anything.
All the other shell scripts are working fine with the help of PHP exec command except ldap_search.
Is there some additional task left for me to do?
Is ldap_search and exec/shell_exec not compatible with each other?
You must use echo exec('your command or script');
Make sure to have permissions to run it. I mean, the web user must have permissions to execute that.
May seem obvious, but I think your failure is in something basic like this. You must put echo to show the result of the command.
EDIT After reading your new comments about it and using that new info... I saw you are trying to redirect the output to a file... but maybe you have 2 different problems.
Have the user which is executing php (usually www-data) permission to write on the folder where the php is?
Your code has quotes inside quotes that must be escaped using . Try this:
<?php exec("ldapsearch -x -v -h 'LDAP://server' -p '389' -D 'uid=\"domain_user_id\",ou=users,ou=internal,o=\"organization\"' -w 'domain_password' -b 'ou=users,ou=internal,o=organization' 'uid=person's_user_id' >> result.txt"); ?>
So you don't need echo if you want the output in a file. And the redirection >> can be inside the command executed, not in php.
Remember that > replaces de file and what you have >> add at the end of the file.
In Python, we can add the command line log to a file instead of the console using this command:
python script.py >> mylogfile.txt
How can I do it using PHP? I've tried
php script.php >> mylogfile.txt
but it doesn't work.
I use Windows 10.
I finally found the answer. It's based on the article PHP on the Command Line – Part 1 Article
I first used php script.php > mylog.txt which returns some of the log text to the console, so I thought it's not writing to the log, but it does. I wanted php script.php > mylog.txt 2>&1 which will add any log to the file.
The article says it doesn't work in Windows, but I use Windows 10 and it works.
The error messages are mixed with the normal output as before. But by
piping the output from the script, I can split the errors from the
normal output:
php outwitherrors.php 2> errors.log
This time, you’ll only see these messages:
Opening file foobar.log Job finished
But, if you look into the directory in which you ran the script, a new file called errors.log will have been created, containing the error
message. The number 2 is the command line handle used to identify
standard error. Note that 1 is handle for standard output, while 0 is the handle for standard error. Using the > symbol from the command line, you can direct output to a particular location.
Although this may not seem very exciting, it’s a very handy tool for
system administration. A simple application, running some script from
cron, would be the following:
php outwitherrors.php >> transaction.log 2>> errors.log
Using ‘>>‘, I tell the terminal to append new messages to the existing
log (rather than overwrite it). The normal operational messages are
now logged to file transaction.log, which I might peruse once a month, just to check that everything’s OK. Meanwhile, any errors that need a quicker response end up in file errors.log, which some other cron job might email me on a daily basis (or more frequently) as required.
There’s one difference between the Unix and Windows command lines,
when it comes to piping output, of which you should be aware. On Unix,
you can merge the standard output and standard error streams to a single destination,
for example:
php outwitherrors.php > everything.log 2>&1
It reroutes standard error to standard output, meaning that both get
written to log file everything.log.
I am attempting to execute a Perl script through PHP via the following command:
$last_line = exec('/usr/bin/perl /path/to/perl/script.pl ' . escapeshellarg($argument),$output,$status);
The script does not perform its function, and the exit status is always 2 (improper use of shell builtins). Both Perl and the script can be read and executed by any user. Running the script on a command line works just fine. Any thoughts?
I use this code to run file.pl located in same folder as php file
$output = passthru("perl relative_path/file.pl ".$_POST["var1"]." ".$_POST["var2"]);
I use a perl script as a database interface from PHP with the following code:
exec ( $cmdToPass . " 2>&1" , $output , $returnVar);
Where:
$cmdToPass is the command I'm executing (the perl script) with stderr redirected to stdout.
$output holds the command's output
$returnVar holds the exit code
You should then be able to print $output to determine why the script. You may also wish to check the PHP error log (usually the same as the web server's error log) to see if anything is captured there.
See the PHP manual on exec if you need more info on the exec function.
I am using the PHP system function on windows as follows:
system("mysql --user=root --password=xxxx --host=127.0.0.1 --verbose <create.sql > out.txt 2>&1", $retVal);
But the error messages apppear at the top of the file. Is it possible to get the error messages to appear after the appropriate line of SQL that has the error.
The problem is that stdout is buffered and stderr is not meaning that the errors will always show first in this case. You are going to need to wrap your system call in a script and sort it afterwards if you wish to retain some sort of order. There is some explanation available over here.
I am using exec function to execute the specific executable files in php .
exec ( $file , $output , $return_value ) ;
When the given file executed successfully I am able to get the output in the second argument
by checking the return values , So, It is working fine. But My requirement is when the command execution getting failed due to some reason. I need to get that error message of that executed program . What I need to do to get the error . through second argument we can get the successful output only . Not error message.
Thanks.
The second argument $output only captures STDOUT from your executable. Error messages are usually sent to STDERR so that they easily can be written to an error log or similar, but this means that you won't see them when you call exec.
If this is a linux system, you could append 2>&1 to your command, in order to redirect STDERR to STDOUT. I haven't tried this, but it should forward the error messages to your $output variable.
Edit:
I've read up on it on www.php.net/exec, and it seems this would work.
exec($file.' 2>&1', $outputAndErrors, $return_value);
It is also possible to redirect the errors to a temporary file and read them separately.
exec($file.' 2> '.$tmpFile, $outputOnly, $return_value);
Edit 2
It seems windows also uses this Bourne style output redirecting syntax, so the examples should work for windows too.
More on input and output streams
If you need to keep stderr and stdout separate, try proc_open:
http://php.net/manual/en/function.proc-open.php
The $return_value will have the error code returned by the program which should be meaningfull enough, I don't think you can have better.