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);
Related
I need to run this exact shell command from PHP shell_exec():
/usr/bin/paste -d'|' <(echo $(id)) <(echo $(pwd))
From a VPS shell (Debian) it works perfectly. However, from shell_exec() it doesn't work:
$command = "/usr/bin/paste -d'|' <(echo $(id)) <(echo $(pwd)) 2>&1";
$output = shell_exec($command);
print_r($output);
It doesn't output anything, the output is just blank/empty.
I am using PHP 7.3 with safe_mode = On and open_basedir, shell_exec() is enabled.
How can I run that command with shell_exec()?
I think the less-than "<" char or "<()" create problems?
perhaps escapeshellcmd would help ?
$command = "/usr/bin/paste -d'|' <(echo $(id)) <(echo $(pwd)) 2>&1";
$escaped_command = escapeshellcmd($command);
$output = shell_exec($escaped_command);
print_r($output);
good luck.
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?
one.sh
#! bin/bash
command="cp 357.svg 000.svg"
echo "Executing Command";
exec $command;
from shell by executing sh one.sh runs perfact and even in php shell_exec("sh one.sh"); works fine.
two.sh
#! bin/bash
command="/usr/bin/inkscape -f 357.svg -e 357.png"
echo "Executing Command";
exec $command;
From shell sh two.sh works fine
but using php shell_exec("sh two.sh") not executing
can any one please tell why it is not executing?
try :
echo shell_exec("sh two.sh 2>&1;")
and see what the output is, maybe it will give you a permission denied error.
Maybe also worth checking which user you are running with (probably something like www-data)
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
Here is my php code
$command = "C:\Program Files\ClustalW2>clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln";
exec($command);
When i run the command using the cmd, it generates the desired file. However when i try passing the same command via my php code it generates no result. How do i fix this problem?
Probably it's because of the > symbol before executable's filename? Also, try with single quotes:
$command = 'C:\Program Files\ClustalW2\clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln';
exec($command, $output, $retval);
var_dump($output);
var_dump($retval);