1

I need to know that If I call a php script execution command through exec command and the script execution fails due to any reason say "file not found" , then how can I find it out. I have following command :

    $cmd="php testfile.php" ;
    $outputfile="testoutput.txt";
    exec(sprintf("%s > %s 2>&1 & echo $!", $cmd, $outputfile),$pidArr, $status);

exec command return -1 in case of error but in this case exec is executing successfully ie $status is coming 0 in my case but the "php testfile.php" command is failing, the output is getting in testoutput.txt. But I need to know the way so that I can identify it after exec if the command is failed. I could think of the option of reading testoutput.txt and grep for fail or error word, but I dont think it is reliable.

Thanks in advance!

2 Answers 2

2

You can receive the output result of the exec function by passing an optional second parameter:

So you could execute the exec() with that 3rd arg, then check if it is non-zero for an error condition.

exec("blah blah blah", $output, $result); 
if($result > 0) 
{ 
   die('error message here'); 
}  

If you don't find the error through that second parameter, you can search for the error log of apache, for example in Ubuntu Server 12.10 through the command $ tail /var/log/apache2/error.log

let me know if i can help you more.

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

6 Comments

I tried it, actually it third argument is giving 0 in my case, file it was an file not found error
so might be you should have to find apace error or any other exception handling method to trace it.
Yes, I am trying out the same thing but thought to find if its the only method for the same.
I am getting error "Could not open input file" in testoutput.txt I mentioned in the code, not even in apache error.
AH! Of course. echo $! executes successfully, which makes it return 0. Use && echo $! and it will only execute the echo if the original command is successful.
|
1

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

exec(sprintf("%s > %s 2>&1 & echo $!", $cmd, $outputfile),$pidArr, $status);

$status=0 if no errors, > 0 if errors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.