Reading STDOUT from aria2c in PHP script

1k Views Asked by At

Here is simple PHP script:

<?php
is_resource($process = proc_open('aria2c http://speedtest.rinet.ru/file100m.dat',  [
   ['pipe', 'r'],
   ['pipe', 'w'],
   ['file', '/tmp/error-output.txt', 'a']
], $pipes, '/tmp', [])) || die();
fclose($pipes[0]);
fpassthru($pipes[1]);
fclose($pipes[1]);
proc_close($process);

The problem is that progress data in output is "stalled" until aria2c is finished. When aria2c process ends, it immediately bursts all output to my script. It is not related with fpassthru(), I've tried plain fread() with the same result.

The flow:

[NOTICE] File already exists. Renamed to /tmp/file100m.dat.4.

<...huge delay and then burst...>

[#edb1dc 70MiB/100MiB(70%) CN:1 DL:8.4MiB ETA:3s]

[#edb1dc 81MiB/100MiB(81%) CN:1 DL:9.7MiB ETA:1s]

[#edb1dc 92MiB/100MiB(92%) CN:1 DL:10MiB]

I need to get lines like "[#edb1dc 92MiB/100MiB(92%) CN:1 DL:10MiB]" without having to wait aria2c to end to gather information about current progress.

Not to mention, if I run the exact same command in console aria2c works fine .

1

There are 1 best solutions below

0
On

I ran into the same issue while trying to monitor the Aria2 download progress (readout) from PHP.

The issue is caused by a missing fflush(stdout) in the Aria2 code but luckily there's a fix you can use in your PHP code to reconfigure the STDOUT buffering mode. You can use the stdbuf utility under Linux or the unbuffer utility under OSX to disable buffering like so:

proc_open('stdbuf -o0 aria2c ...'); // on Linux

or:

proc_open('unbuffer aria2c ...'); // on OSX

(Thanks to @hek2mgl for this answer on another similar question)