PHP view exec status in realtime, is it possible? - php

I am using ubuntu and mktorrent, I am wondering is it possible to get the output from my mktorrent command to my php app live? So I can watch the status of the creating torrent?
At the moment, exec just sits there and waits (sometimes for over an hour :O) for the torrent to be finished making.
Does the php proc open command do something similar to this or do I have to figure out some sort of crazy ajax with a screen session setup?

PHP executes completely on the server and the rendered page gets sent to your browser. In order to show any progress and have it update, you'd have to request it from the server repeatedly (or have the server send progress through sockets or long polling or something, but the idea is the same)
You'll need a way for the server to issue progress updates from the command and some sort of JS (AJAX) to communicate with the server to get those updates.
If you can have the mktorrent command output progress to a file, you can have php read the file on page load and echo it to the user. You can use a simple meta refresh tag to refresh the progress page after a few seconds.
See Asynchronous shell exec in PHP for info on having PHP execute a shell command asynchronously so your PHP script doesn't have to wait for it to finish, causing your page to hang until the task is done.

Related

How can I track a PHP script that is being fired by browser in shell?

I'm firing a php script with jQuery from an HTML interface I make to make things easier. I set the script so I can keep running even if the browser session is lost so I can close the browser.
I wish to track which scripts are currently running in shell (using 'ps -aux' or htop..) but do cannot locate these tasks under the name of the script or with PHP in the process name. Any suggestions? also, what would be the best way to push these tasks into some workload/jobs manager ?
Thanks

How to display progress bar in PHP exec functions

I am running an external script in PHP using its exec() function. I was looking for various options to create a progress bar. I am able to create a plain rotating loader through AJAX but I couldn't achieve the percentage progress bar. Is there any way to do that?
Depending on the program you want to execute, you could use proc_open() instead of exec(), so you can proccess the output, calculate a percentage and throw it back to your ajax script using ob_flush() and flush().
It's not true that you need to wait for the execution to finish before sending any output, but there are some caveats with your server cache configuration and browsers rendering engines, so it is not so reliable.
If you're not using Websockets (the clean and modern option, which can be achieved with PHP using Ratchet or with nodejs using various options), the most reliable way of doing what you want is with polling.
Briefly, instead of calling your script once, you do a first ajax request to init the proccess and then start poking the server again and again to ask the execution status of your script.
For more information, take a look at those answers:
Run process with realtime output in PHP
PHP - Flushing While Loop Data with Ajax
Grab results from a php exec() while the command is still running?
PHP runs on the server, thus can not achieve this (to my knowledge),
here are some answered questions that might be able to help you.
How to show loading status in percentage for ajax response?
Jquery:: Ajax powered progress bar?

How can I execute a PHP script without waiting for completion before the page loads for the end user?

I am working on a site plugin that takes advantage of the YouTube API to grab some data for a specific channel. The problem is that the code takes several seconds to load on larger channels meaning the user has to wait quite a bit of time for the script to finish loading before they can view each page. Most of the time the data retrieved isn't even relevant to them but the script still needs to run its checks. So how can I initialize the PHP script without the user having to wait for it?
My first thought is to use AJAX and make a request to the remote script but will that cause parts of the page to hang as the script is running? I don't need the results from the script, I just need the script to run and do its own thing.
You can use exec to start a separate PHP script. And adding & at the end will make exec return imidiately.
Let script.php do the heavy stuff and call it from a mall script like so:
exec("php script.php > /dev/null &");
You need to have a *nix server environment. Don't think it will work on Windows platform.
AJAX stands for Asynchronous JavaScript and XML, which means that, when using it, the execution of the rest of the code will not be blocked.
You can make an AJAX request to the script you want to execute and then keep rendering the page normally. The AJAX request will be performed in the background, and won't be of any disturb whatsoever.

Is it possible for shell script to detect whether JavaScript is running on firefox?

I wrote an HTML page with JavaScript, a PHP file and a shell script
they are all on the same machine
I run the shell script, it will open the html page with Firefox,
when the JavaScript finishes, it will POST to a getdata.php
<form id="hidden_form" method="POST" action="getdata.php" ></form>
The getdata.php will do something and then it will send a signal to the shell script
the above is the normal behaviour, I'm afraid at some time, the PHP or JavaScript run into error and don't send signal to the shell script
are there any good and simple way for shell script to detect whether JavaScript is running?
a guy below mentioned that I can let javascript send request to the server periodically, like once every 2 minutes, but how to let shell script notice this/get the signal?
As someone suggested, you can poll the server with setInterval to see if the javascript engine is still running, but that might as well mean that only the javascript thread doing setInterval is the one actually running (depending on what kind of error could arise). You could do the post to "status.php" which would touch/write to a file on the disk, which the bash script could poll to see if it's updated in regular intervals.
I'd however suggest that you look into something like phantomjs which allows you to solve these kind of problems.
No.
Remember: Javascript is running remotely over on the client browser; PHP (and your shell script) are on the server.
What you can do is set up an alarm (on the server) when you send the web page, and invoke an alarm handler if it triggers:
http://www.woodwose.net/thatremindsme/2011/05/forking-parallel-processes-in-bash/
If your JavaScript is running a long-to-process script, you may intermittently submit a request to the server over a set interval. If you don't see the request after a set period... ( interval x2 or w/e ) and the signal has not been received upon completion, you can assume that the JavaScript itself has stopped running / broke.
setInterval("methodofcommunicatingtotheserver", 1000);
etc.
..as stated before: No.
Your best options in this case are, either:
Re-design your script. Is it absolutely necessary for it to send the request to the php page via the javascript in-browser?
If the answer to the above is yes, have a look at Selenium, especially Selenium WebDriver.
It does not provide bindings for bash scripts out-of-the-box, but it's pretty easy to use from other languages such as Python (or PHP, they say).

Need to run a long php script from a browser

I created a script that gets data from some web services and our database, formats a report, then zips it and makes it available for download. When I first started I made it a command line script to see the output as it came out and to get around the script timeout limit you get when viewing in a browser. But because I don't want my user to have to use it from the command line or have to run php on their computer, I want to make this run from our webserver instead.
Because this script could take minutes to run, I need a way to let it process in the background and then start the download once the file has been created successfully. What's the best way to let this script run without triggering the timeout? I've attempted this before (using the backticks to run the script separately and such) but gave up, so I'm asking here. Ideally, the user would click the submit button on the form to start the request, then be returned to the page instead of making them stare at a blank browser window. When the zip file they exists (meaning the process has finished), it should notify them (via AJAX? reloaded page? I don't know yet).
This is on windows server 2007.
You should run it in a different process. Make a daemon that runs continuously, hits a database and looks for a flag, like "ShouldProcessData". Then when you hit that website switch the flag to true. Your daemon process will see the flag on it's next iteration and begin the processing. Stick the results in to the database. Use the database as the communication mechanism between the website and the long running process.
In PHP you have to tell what time-out you want for your process
See PHP manual set_time_limit()
You may have another problem: the time-out of the browser itself (could be around 1~2 minutes). While that time-out should be changeable within the browser (for each browser), you can usually prevent the time-out user side to be triggered by sending some data to the browser every 20 seconds for instance (like the header for download, you can then send other headers, like encoding etc...).
Gearman is very handy for it (create a background task, let javascript poll for progress). It does of course require having gearman installed & workers created. See: http://www.php.net/gearman
Why don't you make an ajax call from the page where you want to offer the download and then just wait for the ajax call to return and also set_time_limit(0) on the other page.

Categories