Run server-side js from php through exec() - php

I have a site running on Apache/PHP, and as a matter of performance, I wrote a javascript to do a specific task.
I have installed node.js on server, in order to run this javascript. When I call the script from the command line, it works fine. See the command below:
> node myscript.js
But I need it to run from a php page, and I am trying to do this by calling the exec() PHP function, like this:
<?php exec('node myscript.js >/dev/null/ 2>&1 &'); ?>
...but it's not working.
Am I doing something wrong? Is there another way to do what I want?

I found a way to make it work! I just wrote the full directory where node.js is installed in the exec() call. Simple as that:
<?php exec('/home/bin/node myscript.js >/dev/null/ 2>&1 &'); ?>

Related

How to automate a command with PHP in web app

I have a directory that when accessed through the terminal, I run the command 'make' then 'make install' which subsequently builds a dictionary file. I want to automate this process, which will kick off when the user selects a button on the interface.
Using PHP in my web app I want to navigate to the directory which I have done so here:
chdir('../DictionaryFolder');
Then, I thought this PHP command would run the make and make install:
exec(make);
exec(make install);
But this does nothing.
Any help will be much appreciated!
You need to write the command like the following,
<?php
$output = shell_exec('make;make install;');
echo "<pre>$output</pre>";
?>
Shell exec will do the trick by calling $output via pre tag.
Exec() try to execute PHP like eval() in JS, shel_exec execute commande like you with CLI

How to run a Node.js script from PHP with XAMPP?

I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.

External program in PHP

I'm trying to make a simple script in PHP which download a video of youtube, at the first moment I tried some classes I found on web but unsuccessful, so I decide to use youtube-dl program and call it from to my script.
The big problem is: apparently the process is killed when the page loads in the browser and the download is interrupted.
The most curious thing is that if I execute the script like that: php page.php, the script works nicely but the browser doesn't work.
I note the same thing with wget command, the process also killed.
The code is something like:
<?php
exec("youtube-dl -o /var/www/YT/video.flv https://youtube....");
?>
and
<?php
exec("wget http://link");
?>
*Both youtube-dl and wget are in the same directory from script, I tried too redirect output to /dev/null and fork process mas both no success.
I would try executing it at the background.
<?php
exec("youtube-dl -o /var/www/YT/video.flv https://youtube.... > /dev/null 2>&1 &");
?>
If that works then what it's happening is that your php script ends before youtube-dl

PHP "Cancel" line of code

I am using phpseclib to ssh to my server and run a python script. The python script is an infinite loop, so it runs until you stop it. When I execute python script.py via ssh with phpseclib, it works, but the page just loads for ever. It does this because phpseclib does not think it is "done" running the line of code that runs the infinite loop script so it hangs on that line. I have tried using exit and die after that line, but of course, it didnt work because it hangs on the line before, the one that executes the command. Does any one have any ideas on how I can fix this without modifying the python file? Thanks.
Assuming the command will be run by a shell, you could have it execute this to start it:
nohup python myscript.py > /dev/null 2>&1 &
If you put an & on the end of any shell command it will run in the background and return immediately, that's all you really need.
Something else you could have also done:
$ssh->setTimeout(1);

Can't execute PHP script using PHP exec

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!

Categories