I have installed Sage math on my ubuntu server. I can run sage commands by ssh terminal such as
$sage:
$sage: f = x^2
$sage: f.diff(x)
I would like to do on a php script
exec('sage');
exec('sage: f = 5x^3');
$fprime = exec('sage: latex(f.diff(x))');
echo $fprime;
I would expect "15x^2" as output but that's not happening .. however on ssh terminal all is good..
all help would be greatly appreciated..
I'm not completely sure of what you want to achieve. PHP's exec function executes a shell command, and that's all. Read http://fr2.php.net/manual/en/function.exec.php.
sage: f = 5*x^3 is not a shell command. You can run sage scripts directly via the command line using the -c switch :
./sage -c 'print latex((5*x^3).diff())'
will print 15 \, x^{2} to the terminal. In PHP you can grab this output by passing a second argument to exec:
exec("./sage -c 'print latex((5*x^3).diff())'", $output);
echo $output[0];
Related
I have a simple command using gdal_calc that only works directly in the mac terminal. But, when I run in PHP using exec or in python, nothing happened.
I have a simple code like this :
gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"
In PHP:
exec('gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"', $output, $response);
this doesn't do anything so i run it direclty using python script
#!/usr/bin/env python
import subprocess
subprocess.call('gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"', shell=True);
no error and no result. When i use gdal_grid or gdalwarp it works well in both languages
I'm trying to list running services on a windows server via php. Therefore I'm using shell_exec with winexe.
My script:
$cmd = "winexe --interactive=0 --user='***' --password='***' //192.168.***.** \"net start\"";
$output = shell_exec($cmd);
echo $output;
Unfortunately on execution the page loads forever with no result. The command works on the command-line (Debian).
Anyone an idea?
Thanks in advance.
Save $cmd with correct format into a new bash file. Set cmd value for call this file. Remember set execution perms to this file.
Check if your apache user has perms for exec winexe
===
Try to launch
cat </dev/null | winexe --interactive=0 --ostype=1 --user=...
I'm trying to execute a shell command from php using the exec command
The following command works
echo exec('whoami');
but I need to execute the following shell command
cat > /var/www/myfolder/abc.txt
hi
hello
welcome
the problem is that the above shell command has to be executed in multiplelines. The words "hi", "hello", "welcome" are dynamic and come from php variables. Is there a workaround for this problem.
To execute a multiline shell command in php:
passthru("bash <<'END'
echo \"executed in new bash session\";
pwd;
whoami;
exit;
END
");
This way you could for example connect to a ssh server using php and then execute some commands there
I don't understand what you mean by cat > abc.txt. This command will empty the file.
I am using this code on Ubuntu 13.04,
$cmd = "sleep 20 &> /dev/null &";
exec($cmd, $output);
Although it actually sits there for 20 seconds and waits :/ usually it works fine when using & to send a process to the background, but on this machine php just won't do it :/
What could be causing this??
Try
<?PHP
$cmd = '/bin/sleep';
$args = array('20');
$pid=pcntl_fork();
if($pid==0)
{
posix_setsid();
pcntl_exec($cmd,$args,$_ENV);
// child becomes the standalone detached process
}
echo "DONE\n";
I tested it for it works.
Here you first fork the php process and then exceute your task.
Or if the pcntl module is not availabil use:
<?PHP
$cmd = "sleep 20 &> /dev/null &";
exec('/bin/bash -c "' . addslashes($cmd) . '"');
The REASON this doesn't work is that exec() executes the string you're passing into it. Since & is interpreted by the shell as "execute in the background", but you don't execute a shell in your exec call, the & is just passed along with 20 to the /bin/sleep executable - which probably just ignores that.
The same applies to the redirection of output, since that is also parsed by the shell, not in exec.
So, you either need to find a way to fork your process (as described above), or a way to run the subprocess as a shell.
My workaround to do this on ubuntu 13.04 with Apache2 and any version of PHP:
libssh2-php, I just used nohup $cmd & inside a local SSH session using PHP and it ran it just fine the background, of course this requires putting certain security protocols in place, such as enabling SSH access for the webserver user, so it would have exec-like permissions then only allowing localhost to login to the webserver ssh account.
I am trying to run a program on swi prolog through php. I am running on windows
My command are as follow
$cmd = "C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
exec( $cmd );
nothing happens when i try to run my php code it waits for while and returns to command prompt on windows/system32/exe
but when i execute the same command on cmd line i get the desired out put
C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
i had been trying it for last two days, any help will be highly appreciated
Thanks in advance for all people who will give it a thought and help me
Try using double-backslashes (and including "halt" within the double-quotes):
$cmd = "C:\\Program Files (x86)\\pl\\bin\\swipl -f test1.pl -g test halt";
Program Files (x86) => progra~2
$cmd = 'C:\progra~2\swipl\bin\swipl.exe -s C:\prolog\web\base.pl -g test,halt';
it's work for me