How to show complete output even though it's error? - php

I have following php script in which I am trying to execute a command on Windows and print it's output. I have tried system(), shell_exec(), passthru() and exec().
$cmd = $_POST['cmd'];
$result = array();
exec($cmd, $result);
foreach ($result as $line ){
echo $line."<br>";
}
exec($command, $array) is the closest to my expectation of printing everything when a command is executed. I wish to print everything even though it's an error. But it only prints when command is executed successfully.
How to achieve it ?

$result only contains the standard output of the command, but error messages are usually written to standard error. You need to redirect stderr to stdout when running the command.
$cmd = "$cmd 2>&1"
exec($cmd, $result);
In the shell, what does " 2>&1 " mean?

Related

How to run for loop that is inside shell script file from PHP file?

I have a a shell script (myscript.sh), which contains many bash commands. I want to run these command from a php file (index.php).
I used exec to execute the commands as following:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
exec($contents, $output, $return_var);
print_r($contents);
print_r($return_var);
print_r($output);
Here is the content of myscript.sh
VAR1=("somePath/allDirs")
ls
cd ..
ls
for DIR in "${VAR1[#]}"; do
echo "Name is $DIR"
done
pwd
....... other 5 lines of commands
All commands before the loop is working i.e: ls and cd .. and I got the output. However, the for loop is not working and it stop executing the rest of the commands after it.
My question is: how to execute this for loop that is inside shell script file from php file?
note: the for loop contains more command here I just put the echo to make the code easy to read and clear.
Are you mutating the contents of the script at all? Why not just execute the script file rather than passing in the contents of the script? Regardless, if you must pass the contents directly to exec, pass it to bash -c and escape the command contents:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
$contents = escapeshellarg($contents);
exec("bash -c $contents", $output, $return_var);

shell script with SFTP from PHP

I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}

How to execute crontab -l via php webpage? Linux Permission

I was wondering if it is even at all possible to execute an command such as "crontab -l" via webpage? This seems to be an permission issue.
$out = shell_exec('crontab -l');
echo '<pre>'.$out.'</pre>';
When this is executed via PHP, the value of $out is empty.
From PHP.net :
It is not possible to detect execution failures using this function.
exec() should be used when access to the program exit code is
required.
I suggest you use exec so that you can check if the command executed successfully. You should be able to output any data.
Something like:
exec('crontab -l 2>&1', $output, $return_code)
if ($return_code !== 0) {
//Returned Error
echo implode("\n", $output); // display output
} else {
//Returned OK
echo implode("\n", $output); // display output
}
Well this is what I ended up doing...
I added a a cron job to update a txt file with the outout of crontab -l every minute
crontab -l > /var/www/html/logs/cronJobs.txt
Set the permission of cronJobs.txt
Link the cronJobs.txt from the PHP page

How to find if command executed in exec command fails?

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!
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.
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

running a script exec command from php

I have an application where I need to run a PHP script by exec() function of php.
"php
/var/www/server/data/scripts/ThreadHandler.php
145596 >
/var/www/server/data/logs/threads/thread.145596.log
2>&1 &"
also tried
"php
/var/www/server/data/scripts/ThreadHandler.php
145596 >
/var/www/server/data/logs/threads/thread.145596.log
2>&1 "
I am running above command with exec() function of PHP, but it is not getting run, I can't track any error. please suggest any change.
try
passthru('php -f /var/www/server/data/scripts/ThreadHandler.php 145596 >
/var/www/server/data/logs/threads/thread.145596.log 2>&1');
Also, try to run it from linux command line
Try defining $output and $return_var arguments for exec and print their values :
$output = array();
$rv = null;
exec("your command", $output, $rv);
print_r($rv);
print_r($output);

Categories