body being the data sent over RabbitMQ. * * @param AMQPMessage $msg */ public function execute(AMQPMessage $msg) { $data = unserialize($msg->getBody()); if (isset($data['id']) && isset($data['cmd'])) { $taskloggerId = $data['id']; $cmd = $data['cmd']; $file_name = $this->createTaskLoggerCmdFile($taskloggerId, $cmd); $output = $this->runFileProcess($file_name); return true; } return false; } /** * @param string $taskloggerId * @param string $cmd * * @return string */ public function createTaskLoggerCmdFile($taskloggerId, $cmd) { $mode = 0777; $tasklogger_dir = "/tmp/tasklogger/{$taskloggerId}/"; if (!file_exists($tasklogger_dir)) { mkdir($tasklogger_dir, $mode, true); } $file_name = $tasklogger_dir.'cmd.sh'; file_put_contents($file_name, $cmd); chmod($file_name, $mode); return $file_name; } /** * @param string $filename * * @return string */ public function runFileProcess($filename) { $process = new Process($filename); $output = null; try { $process->mustRun(); $output = $process->getOutput(); } catch (ProcessFailedException $e) { $output = $e->getMessage(); } return $output; } }