Run exec() Rscript from wordpress install - php

Goal:
Run a simple Rscript from a wordpress page.
I'm currently attempting to run an Rscript using exec() upon loading the page. The script creates a histogram of 100 random samples from integers 1 through 10, writes system time to title and saves figure to .png file.
Setup:
Running Wordpress install on Ubuntu EC2 micro instance
R has been successfully installed and tested through ssh
Using Exec-PHP Wordpress plugin so that PHP code can be written and executed (tested successfully)
PHP code (within wordpress Page)
<?php
echo "This is the Exec-PHP 'Hello World'\n";
echo exec("date");
?>
<?php
exec("Rscript <PATH>/test.R");
?>
<img src="<Image Location>/samplePlot.png" alt="" title="Sample R" />
Rscript - test.R
png( "<Image Location>/samplePlot.png")
hist( sample( 1:10, 100, replace = TRUE), main= Sys.time(), lwd = 5)
dev.off()
The image file loads but it is not updated, indicating the Rscript was never executed. I've isolated it down to that being the issue but unsure why that is.
How can I debug this? I don't really know any PHP but I tried the following:
<?php
exec("\usr\bin\Rscript <PATH>/test.R", $output, $result);
echo $output;
echo $result
?>
Which returns:
Array2
I was hoping to get the command line output to check for errors. Is this possible?

I would think that the problem is that you did not specify the full path to Rscript and the user running PHP/Apache just does not know where to search for it.
Update that exec command like (on Linux):
exec("/usr/bin/Rscript <PATH>/test.R");
Anyway, I would suggest installing littler for the task later and (based on that) runnning r instead of Rscript for letting things run a lot faster - if installing eg. rApache is not an option.

To get an idea of the problem try:
$e = exec("\usr\bin\Rscript <PATH>/test.R 2>&1");
var_dump($e);
If you get something like: Error in dyn.load(file, DLLpath = DLLpath, ...) ...
you probably need to update the servers dynamic libraries (try searching for libfreetype.dylib), or if your running MAMP (as I am) you need to comment (#) the two uncommented lines in: /Applications/MAMP/Library/bin/envvars
I know it is a long time since you posted the question, but I spend a lot of time with a similar problem - hopefully somebody can save some time ;)

Related

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.
What I am trying to do:
I am trying to make PHP call a virtualenv’d install of the Newspaper lib output text when I call it.
What I have now:
PHP: (updated)
<?php
$output = exec('newspaper2/bin/python3 /var/www/html/components/python/test.py 2>&1', $output2);
print_r(error_get_last());
echo $output2;
echo $output;
…this works when using a non-virtualenv script
Python: (updated)
from newspaper import Article
url = 'http://example.com/'
article = Article(url)
article.download()
article.html
article.parse()
article.authors
article.publish_date
string = article.text
print(string)
What the issue is:
I can run the script that PHP is running from the command line and it outputs just fine.
What I have tried:
With PHP, (I have tried all the “exec” calls for PHP) it cannot seem to open the virtual environment and returns nothing.
Before the script I have called “python3” and a few other things to no avail.
Yes, I have chmoded it to be executable…
I feel like this should be so simple.
I have tried suggestions on other posts and all over the web to no avail.
Questions:
Did I set up the virtualenv wrong?
At the top of the Python script, instead of the “#!/usr/bin/env python3” should I call something else?
If so, where do I find it? Should I start from scratch and will that
help?
Thank you for your help;
PS: I am running Ubuntu16, PHP7 and I need to use Python3
In the virtualenv'ed scripts (i.e. installed via the setuptools' entry-points), you should not touch the shebang (#!... first line). It is populated by the virtualenv & setuptools & related tools.
If you specify your own shebang, then it is not virtualenv'ed script. In that case, call python directly:
exec('/path/to/venv/bin/python3 /var/www/html/components/python/testing.py');
Alternatively, you can put the absolute path to the virtualenv's python binary to the py-script, but this does not look a good idea.
Also, remember that virtualenvs are non-relocatable. So they should stay in the path where they were created.
Also note that exec() returns only the last line of the output. You probably want shell_exec() or exec('...', $output) to get the whole output.
Also, it is unclear what happens with your script, and what is being printed on stderr. Try this command to see what is the error:
exec('/path/to/script 2>&1', $output)
#OR:
exec('/path/to/venv/bin/python3 /path/to/script 2>&1', $output)
OK, I finally figured it out and learned a lot in the process. The newspaper lib that I am using by default tries to write to the base of the users home directory. In this case, it was attempting to write to www-data, /var/www.
To fix this:
Go to the settings.py file in the newspaper library.
Edit the variable DATA_DIRECTORY = '.newspaper_scraper' and change it to DATA_DIRECTORY = '.path/to/writable/directory'
Save the file and you should be good to go.
I have no idea why it was not returning the errors that would have explained this sooner.
Hope this helps anyone else.
Thank you so much Sergey Vasilyev for your help. I appreciate it greatly.

Slimerjs with exec linux centos 6

I just installed Slimerjs and no results on the web.
the slimerjs is found and commands like -help etc output like they should:
exec('/usr/bin/slimerjs -help'.' 2>&1',$out,$ret);
but this line does not work
exec('/usr/bin/slimerjs /var/www/html/capture.js'.' 2>&1',$out,$ret); var_dump($ret, $out);
outputs only int(1); and nothing more. no errors in logs, it does not work. I can write everything I like if the bath is correct the output is always int(1).
Thank you
Had same issue now, just a minute ago. But I am using a combination of Casperjs with Slimerjs engine (Casperjs is a great tool for working with your slimerjs and phantomjs scripts, in a friendlier enviroment programming).
The working php script:
<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
putenv("CASPERJS_EXECUTABLE=/usr/local/bin/casperjs");
putenv("SLIMERJS_EXECUTABLE=/usr/local/bin/slimerjs");
putenv("DYLD_LIBRARY_PATH");
echo passthru('/usr/bin/xvfb-run /usr/local/bin/casperjs --ssl-protocol=any --proxy-type=auto --engine=slimerjs casperjsscript.js 2>&1');
?>
You could perhaps check to include some of the paths, as indicated seen in my script and make a few changes, or just install Casperjs :)

How to execute a batch script from PHP

Server Specs
Windows 2008Rc2
IIS7
mysql
PHP 5.3
I have a batch file that does a mysql dump and then zips up the contents and makes it available for download in a public folder. Now i know this scripts works because it runs fine every-time i run it manually, but i can't seem to get it to work through php.
Basically I would like to just be able to call a php page that will run this batch file.
I know that exec is enabled as im able to use shell_exec to ping google.com, and i can get the output back.
I've tried with just system() and exec(), but still nothing. In some cases it looks like the page is working, but it just sits on the loading prompt.
I've searched high and low trying a million different combinations of commands, but none of them seem to work for me.
I've been reduce to trying this simple command, as i can get ping to work from this. Although when the page is called it only displays the echo statements.
<?php
echo "Starting...";
echo shell_exec("C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>
I've also tried this, but the page just hangs on a loading screen and doesn't display the echo commands.
<?php
echo "Starting...<br><br>";
system("cmd /c START C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>
Not really sure where to go from here. I've tried pathing the cmd.exe file, but that made no difference. Is there an easier way to do this? Another programming Language perhaps? Any help is appreciated.
Try executing using "shell_exec" with a full path to CMD.exe
<?php
echo "Starting...<br><br>";
shell_exec("c:\WINDOWS\system32\cmd.exe /c START C:\inetpub\wwwroot\DBZIP2");
echo "Success!";
?>

How to catch the result of a background PHP script launched from inside PHP?

I've got some PHP code that I want to run as a background process. That code checks a database to see if it should do anything, and either does it or sleeps for awhile before checking again. When it does something, it prints some stuff to stdout, so, when I run the code from the command line, I typically redirect the output of the PHP process to a file in the obvious way: php code.php > code.log &.
The code itself works fine when it's run from the shell; I'm now trying to get it to run when launched from a web process -- I have a page that determines if the PHP process is running, and lets me start or stop it, depending. I can get the process started through something like:
$the_command = "/bin/php code.php > /tmp/code.out &";
$the_result = exec($the_command, $output, $retval);
but (and here's the problem!) the output file-- /tmp/code.out -- isn't getting created. I've tried all the variants of exec, shell_exec, and system, and none of them will create the file. (For now, I'm putting the file into /tmp to avoid ownership/permission problems, btw.) Am I missing something? Will redirection just not work in this case?
Seems like permission issues. One way to resolve this would be to:
rename your echo($data) statements to a function like fecho($data)
create a function fecho() like so
.
function fecho($data)
{
$fp = fopen('/tmp/code.out', 'a+');
fwrite($fp, $data);
fclose($fp);
}
Blurgh. After a day's hacking, this issue is finally resolved:
The scheme I originally proposed (exec of a statement with
redirection) works fine...
...EXCEPT it refuses to work in /tmp. I
created another directory outside of the server's webspace and opened
it up to apache, and everything works.
Why this is, I have no idea. But a few notes for future visitors:
I'm running a quite vanilla Fedora 17, Apache 2.2.23, and PHP 5.4.13.
There's nothing unusual about my /tmp configuration, as far as I know (translation: I've never modified whatever got set up with the basic OS installation).
My /tmp has a large number of directories of the form /tmp/systemd-private-Pf0qG9/, where the latter part is a different set of random characters. I found a few obsolete versions of my log files in a couple of those directories. I presume that this is some sort of Fedora-ism file system juju that I will confess to not understanding, and that these are orphaned files left over from some of my process hacking/killing.
exec(), shell_exec(), system(), and passthru() all seemed to work, once I got over the hump.
Bottom line: What should have worked does in fact work, as long as you do it in the right place. I will now excuse myself to take care of a large bottle of wine that has my name on it, and think about how my day might otherwise have been spent...

Processing Gem Data

I have a question regarding running a shell command via PHP. My goal is to successfully run compass compile [project] via PHP. I have tried the following:
echo system('compass compile [project]', $s); // prints [31m[0m
echo $s; // prints 1
echo passthru('compass compile [project]', $p); // prints [31m[0m
echo $p; // prints 1
echo shell_exec('compass compile [project]'); // prints [31m[0m
echo exec('compass compile [project]', $e, $ee);
print_r($e); // Array ( [0] => [31m[0m )
echo $ee; // prints 1
I even tried running a shell command to an executable file that contained compass compile test and I still got the same results as the trials above.
My questions
What does [31m[0m mean? Does this represent binary data? Do these represent bash colors as search engines suggest?
As far as I know, the output should be the following:
For kicks, I tried to execute via system(/usr/local/bin/compass compile [project]); and I got the same result. I double checked my path so I know I can execute these commands as expected. Here is the output from echo $PATH:
/usr/lib/lightdm/lightdm:
/usr/local/sbin:
/usr/local/bin:
/usr/sbin:
/usr/bin:
/sbin:/bin:
/usr/games:
/usr/local/games:
/var/lib/gems/1.9.1/bin
Is there a way to compile compass projects using PHP?
I've seen a similar error before.
Typically it is due to the things being output in the bash startup scripts. For example, I had an echo in one of my bash startups that jacked up a lot of scripts till I realized what was causing the problem.
Or, perhaps the user (www-data ?) doesn't actually have a home dir and appropriate startup scripts in place?
You can try this to get a non interactive shell:
exec("/bin/bash -c \"compass compile [project]\"", $e, $ee);
print_r($e);
echo $ee;
If you still have issues, try redirecting the output to a tmp file, an checking it:
exec("/bin/bash -c \"compass compile [project] > /tmp/compass.compile.output\"", $e, $ee);
print_r($e);
echo $ee;
See also: What's the difference between .bashrc, .bash_profile, and .environment?
The issue was fixed by using sass --compass and redirecting the stderr to stdout via echo shell_exec("sass --compass [project] 2>&1");
It was a pretty long and arduous process figuring this out since it's been awhile since I've dabbled in command line programs. Remember that error streams and output streams might be on different outputs. The easiest way to test this is to shovel the output into a file via:
# do this once with a good file and once with a file that will give errors
sass --poll style.scss > output.txt
If output.txt is empty then the error output is on the stderr stream such as the case above. We can correct this by redirecting the stderr to the srdout. For example:
sass --poll > output.txt 2>&1
#shows results
cat output.txt
I created a simple PHP script that redirects the output from one textarea to the other. The code can be found here.
First guess would be a permissions issue. Odds are the user account running PHP (unless you're running this from the command line, I'm guessing that this is the user that the httpd apache daemon is running under) doesn't have the permissions to do what you're asking. The errors are extremely unhelpful, however.
From what I can tell, it looks like an attempt to have the error show up in red on the command line. My guess is that there are hidden (or somehow never printed out) characters in-between the codes. Check out some of your apache and/or PHP error logs to see if anything helpful is showing up there that never made it into the PHP variable. Or, for kicks, try copy and pasting the output with the bash colors into a basic text editor and first delete each character from the beginning one by one... see if anything magically appears. If that doesn't work, try the same in reverse, backspacing from the end. Either way, there's an error occurring, so important that it must show in bold red letters to you, it's just not getting to you.
If it does in fact turn out to be a permissions issue, and it's one you can't remedy through permissions wrangling, you could create an intermediary file that your Apache user has permissions to write to, and your cron user has permissions to read from. Instead of running the code directly from PHP, put it in the file, then run a cron on a frequent basis looking for anything in that file, CHECKING IT FOR VALIDITY, and then running it through the compiler and removing it from the file.
It'd be ugly, but sometimes pretty things don't work.
You guessed it right it is colors but the way it is defined is not right. For more information regarding using colors in console please refer to this document. Also, for compiling SCSS via compass you can use shell_exec command in linux. For more information regarding shell_exec please refer to this document. Let us know how it goes.

Categories