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?

- 3,708
- 6
- 29
- 48
-
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) – sachleen Dec 01 '12 at 06:20
-
That's not strictly true I think, I have a script that downloads a video from youtube which gives me a live view of the progress before the script execution has finished, or is this just because file_get_contents() is non-blocking or something of the sort? – Goulash Dec 01 '12 at 06:35
-
Can't say without seeing the source... it's probably using JS to communicate with the server to get progress. PHP simply cannot do this alone. – sachleen Dec 01 '12 at 06:39
-
:( yeah the ajax gets the filesize on the server periodically, its just initiated at the same time as the active download from the same script.... So I'm going to have to figure out some sort of ajax > screen session setup? – Goulash Dec 01 '12 at 06:43
-
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. – sachleen Dec 01 '12 at 06:47
-
would simply $cmd > output.txt work for getting the info live? and then, how do I stop the exec command to start monitoring output.txt without stopping the command? – Goulash Dec 01 '12 at 06:53
-
See http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php – sachleen Dec 01 '12 at 06:56
-
AH okay, thanks :P would be nice if you could put something into an answer so I can mark this question as answered :) – Goulash Dec 01 '12 at 07:04
1 Answers
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.
-
Not completely true, see [`flush()`](http://www.php.net/manual/en/function.flush.php). – HellaMad Dec 01 '12 at 07:28
-
@DC_ you're right, hadn't thought of that. Although if you use flush you have to know some browsers expect certain number of characters before outputting anything. If you put that into a complete answer I'd +1 that. – sachleen Dec 01 '12 at 20:29