Yep, that's always a catcher. I don't see it here, but it doesn't mean it doesn't exist.
I see you printed the $cmd here, so that's a good start. Drag that up from the HTML source itself (not from the browser window), and paste that into the command line. If that's good, we can look at the next step.
Does it ever make it to the echo that indicates the end and the $out, or does it just hang there and timeout eventually?
header('HTTP/1.1 420 Enhance Your Calm');