/** * Executes locally a file * * @param type $workdir the working directory * @param type $script_path the script file, with path relative to the study_dir */ function execute_script_local($workdir, $cmd, $study, $jobid) { include 'config.inc.php'; if (chdir($workdir)) { $outfile = get_absolute_path(get_job_output_file($study, $jobid)); # things that are needed to proc_open, see http://it2.php.net/manual/en/function.proc-open.php $pipes = 0; $descriptors = array(0 => array('pipe', 'r'), 1 => array('file', $outfile, 'a'), 2 => array('file', $outfile, 'a')); $env = array(); $env["HOME"] = get_local_exec_dir(OC_User::getUser()); $env["PATH"] = $_SERVER["PATH"]; # use a UNIX command to get the current user name $env["USER"] = exec("whoami"); chmod($cmd, 0755); $cmd = str_replace(" ", "\\ ", $cmd); $ph = proc_open($cmd . " &", $descriptors, $pipes, $workdir, $env); if ($ph) { /** * @var $status array */ $status = proc_get_status($ph); $pid = $status["pid"]; proc_close($ph); // // UGLY HACK , I increase the PID returned by proc_get_status by 1 // why ? because proc_open() spawns a process /bin/sh, that in turn launches our script. // the full command is /bin/sh -c $command $args // Our process is the direct child of such shell process. // The $pid returned above is the PID of the shell process, not the script launched by that shell. // // Since in Unix the child process is always parent_pid + 1, it's mostly safe to increment that value by 1 // to obtain the real PID // if ($pid) { $pid = (int) $pid + 1; $jobinfo = array("jobid" => $jobid, "pid" => $pid, "start_date" => date("Y-m-d H:i:s"), "script" => $cmd, "exec_dir" => $workdir, "exec_type" => "local"); save_job_info($study, $jobid, $jobinfo); // TODO: here we should check if the job is effectively running with that PID } return $pid; } return false; } else { return false; } }
<?php /* * delete_temp_dir * Created on: Jun 5, 2013 9:43:45 AM * * Copyright 2013 EnginSoft S.p.A. * All rights reserved */ include_once 'neurocloud/lib/common.php'; $user = $_GET["user"]; $jobid = $_GET["jobid"]; $referer = isset($_GET["redirect"]) ? $_GET["redirect"] : false; $path = get_local_exec_dir($user) . "/{$jobid}"; if (is_dir($path)) { rmdirr($path); } if ($referer) { header("Location: " . $referer); } exit;
function get_job_exec_dir($jobid) { return get_local_exec_dir(OC_User::getUser()) . DIRECTORY_SEPARATOR . $jobid; }