Open MATLAB script with PHP - php

I need to run a MATLAB script from PHP file.
This is what I tried, which doesn't work:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run('/Applications/MAMP/htdocs/files/appResponse.m')" ';
shell_exec($cmd);
exit;
But this code works:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r ';
shell_exec($cmd);
exit;
I couldn't figure out how to fix the first command to run the .m script file. Any suggestions?

You need to properly escape the quotes. Notice the backslashes:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run(\'/Applications/MAMP/htdocs/files/appResponse.m\')"';
shell_exec($cmd);
exit;
Now the $cmd variable contains a valid path to the Matlab executable, with proper arguments as far as PHP is concerned:
/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run('/Applications/MAMP/htdocs/files/appResponse.m')"

Related

Mount remote file system using sshfs with a PHP script

I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;
This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh

php shell_exec can run command but not sh file

from PHP I can run commands with shell_exec but can't run bash files
I run this command
sudo ls /var/www/
and i get results
/var/www/1.sh
/var/www/2.sh
/var/www/3.sh
but when I run this command nothing happens
$output = shell_exec('sudo sh /var/www/1.sh > /dev/null 2>&1');
echo "<pre>$output</pre>";
In 1.sh i added this code
#!/bin/bash
/usr/bin/echo "test" > TEST1.txt
it works when i type in terminal ./1.sh so only from php is not working
Server: centos
from PHP I think I have root premission to execute commands
this will fix ur problem
$output = shell_exec('sudo /usr/bin/sh /var/www/1.sh > /dev/null 2>&1');
Your bash script has output into the TEST1.txt file, not into console!
Try that:
#!/bin/bash
/usr/bin/echo "test"
Or like that
$output = shell_exec('sudo sh /var/www/1.sh 2>&1');
echo "<pre>" .file_get_contents('/var/www/TEST1.txt'). "</pre>";
That string are wrong!!!!
$output = shell_exec('sudo sh /var/www/1.sh 2>&1');
&1 don't have a reason, because don't have definition
$output = shell_exec('sudo sh /var/www/1.sh > /dev/null 2>&1');
In this case &1 === /dev/null

Php exec() function is not working on heroku with command nice

I am using the following lines of php code in my heroku php app, but these lines are not working :
$exec = "ps -p pid | grep pid.php";
$cmd='nice -n15 ' . php path . ' external.php ';
$cmd.=" > /dev/null 2>/dev/null & echo $!";
$out=array();
exec($cmd, $out);
Could you please suggest any other alternative of this or what mistake this code have?

shell_exec not executing sh file for inkscape command

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)

PHP - run script in background

I have to execute a php script (test.php) in the background. I tried this but it's not working:
<?
$cmd = "php /home/megad404/www/prove/test.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
It returns "Successful" but it doesn't execute test.php
test.php:
<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>
test.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.
Could it be a server issue? Is there another way to run a script in the background?
Use shell_exec and nohup
shell_exec("nohup php /home/megad404/www/prove/test.php > /dev/null & echo $!");
Use shell_exec and give absolute path for php:
$output = shell_exec("nohup /usr/bin/php7.0 -f /home/megad404/www/prove/test.php &> /dev/null &");
Just confirm the absolute path for php in your server. For instance, I'm using php 7.0 and the absolute path is /usr/bin/php7.0
Also, give executable permission to the php file you are running from your code.
chmod +x /home/megad404/www/prove/test.php

Categories