-
Notifications
You must be signed in to change notification settings - Fork 0
/
processClass.php
51 lines (45 loc) · 1.45 KB
/
processClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/*
* simple class to launch an external process, keep track of the PID and status.
*
*/
class Process {
private $pid;
private $command;
private $outputFile;
public function __construct($cl=false, $outputFile=false) {
if ($cl != false) {
$this->command = $cl;
$this->outputFile = $outputFile;
$this->runCom();
}
}
private function runCom() {
if ($this->outputFile != false) {
// program output goes to designated file
$outputFile = ' > ' . $this->outputFile . ' & echo $!';
} else {
// program output goes to dev/null
$outputFile = ' > /dev/null 2>&1 & echo $!';
}
$command = 'nohup ' . $this->command . $outputFile;
exec($command ,$op);
$this->pid = (int)$op[0];
}
public function status() {
$command = 'ps -p ' . $this->pid;
exec($command,$op);
if (!isset($op[1]))return false;
else return true;
}
public function getOutPutFileContent(): string {
// check if output file was set and if exist
if($this->outputFile != false && file_exists($this->outputFile)) {
$command_tail = 'tail -n ' . OUTPUT_FILE_LINES . ' ' . $this->outputFile;
$tail = shell_exec($command_tail);
} else {
$tail = 'designated output file: ' . $this->outputFile . 'not found!';
}
return $tail;
}
}