External program in PHP - 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

Related

How to run external php script using Php shell_exec?

I have the following code on my index.php file. But it is not working properly.. When i directly visit domain.com/script.php it works. I need this script to be executed in the background while accessing index page. Can anyone help me?
shell_exec('php script.php > /dev/null 2>/dev/null &');
check php is running in safe mode or not shell_exec is disabled in safe mode for the sake of security why don't you use
curl
to run the code
Well I think there are 2 possible issues in your case
1) try this:
shell_exec("script.php 2>/dev/null >/dev/null &");
OR
shell_exec("script.php 2>&1 | tee -a /tmp/mylog 2>/dev/null >/dev/null &");
2) A simple way to handle the problem of capturing stderr output when using shell-exec under windows is to call ob_start() before the command and ob_end_clean() afterwards
ob_start();
ob_end_clean();
Well instead of using shell_exec, you can make an ajax call to script.php when the user visits index.php.
Another option is to run the script.php as a cron job every 5 minutes or so. When the user visits index.php, some data can be saved to database indicating that script.php should run. script.php should check if it is marked for running.

php5 make a system call in the background

I am trying to create a tarball from a php5 script under Linux, and I don't really care for the output; the way I have so far found immediately is to simply
system("tar czf tarball.tgz directory/path &");
However I would like to background the process
Checking system() documentation it mentions having to redirect the output to file
However
system("tar czf tarball.tgz directory/path > /dev/null 2>&1");
doesn't help. The system() function does not take a file descriptor... what am I missing?
Testing with these:
script test.php
<pre><?php
exec("bash dodate 2>&1 /dev/null &");
system("echo done at \$(date)");
?></pre>
Script ./dodate
sleep 5
date
I go to my browser and call/refresh the page; it takes indeed 5 seconds thenprints/updates the "done" message.
Thanks
You "don't have" threads in php. One trick you can do is to do a curl request to another php that does what you want. You'll need to make sure that your curl times out pretty soon, and that the other php doesn't die when the http connection to it is closed by the curl timeout.
You can also read about the topic here: cURL Multi Threading with PHP or cURL Multi Threading?

Run server-side js from php through exec()

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 &'); ?>

php system() doesnt let the rest of the code , execute

I have a PHP script like this:
<?php
system("firefox http://run.imacros.net/?m=the_macro.iim 2>&1");
// CODE//
?>
When I run this from terminal, opens my FireFox normally but never run the rest of the code in the script!
If I close the FireFox manually then the script runs the rest of the code.
I want to execute my script without stacking in the system() command.
The 2>&1 redirects stderr to stdout, but you haven't redirected stdout, so if any output is generated to either handle, PHP waits. To discard any output to both handles, use
system("firefox http://run.imacros.net/?m=the_macro.iim >/dev/null 2>&1");

Running PHP from command line

I am trying to manage a queue of files waiting to be processed by ffmpeg. A page is run using CRON that runs through a database of files waiting to be processed. The page then builds the commands and sends them to the command line using exec().
However, when the PHP page is run from the command line or CRON, it runs the exec() OK, but does not return to the PHP page to continue updating the database and other functions.
Example:
<?php
$cmd = "ffmpeg inpupt.mpg output.m4v";
exec($cmd . ' 2>&1', $output, $return);
//Page continues...but not executed
$update = mysql_query("UPDATE.....");
?>
When this page is run from the command line, the command is run using exec() but then the rest of the page is not executed. I think the problem may be that I am running a command using exec() in a page run from the command line.
Is it possible to run a PHP page in full from the command line which includes exec()?
Or is there a better way of doing this?
Thank you.
I wrote an article about Running a Background Process from PHP on Linux some time ago:
<?php system( 'sh test.sh >/dev/null &' ); ?>
Notice the & operator at the end. This starts a process that returns control to the shell immediately AND CONTINUES TO RUN in the background.
More examples:
<!--
saving standard output to a file
very important when your process runs in background
as this is the only way the process can error/success
-->
<?php system( 'sh test.sh >test-out.txt &' ); ?>
<!--
saving standard output and standard error to files
same as above, most programs log errors to standard error hence its better to capture both
-->
<?php system( 'sh test.sh >test-out.txt 2>test-err.txt &' ); ?>
Have you tried using CURL instead?
Unsure but probably thats due to the shell constraints of cron processes if it works as a web page then use it as a web page, setup a cron job that calls wget wherever_your_page_is and it will be called via your web server and should mimic your tests.

Categories