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?
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php