-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBackgroundProcess.php
More file actions
103 lines (94 loc) · 2.65 KB
/
BackgroundProcess.php
File metadata and controls
103 lines (94 loc) · 2.65 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
Title: PHP Script Background Processor
Version: 1.1
Author: Sanjay Kumar Panda
Description: Here we can run a PHP file (script) in background, This process is hidden to the end user. IT improves your Website efficiency.
*/
class BackgroundProcess
{
private $pid;
private $random;
private $command;
private $debugger = true;
private $msg = "";
private $isOutPut = false;
/*
* @Param $cmd: Pass the linux command want to run in background
*/
public function __construct($cmd = null, $isOutPut = null)
{
if (!empty($cmd)) {
$this->command = $cmd;
$this->isOutPut = $isOutPut;
$this->do_process();
} else {
$this->msg['error'] = "Please Provide the Command Here";
}
}
public function setCmd($cmd)
{
$this->command = $cmd;
return true;
}
public function setProcessId($pid)
{
$this->pid = $pid;
return true;
}
public function getProcessId()
{
return $this->pid;
}
public function status()
{
$command = 'ps -p ' . $this->pid;
exec($command, $op);
if (!isset($op[1])) return false;
else return true;
}
public function showAllProcess()
{
$command = 'ps -ef ' . $this->pid;
exec($command, $op);
return $op;
}
public function start($isOutPut = null)
{
$this->isOutPut = $isOutPut;
if ($this->command != '')
$this->do_process();
else return true;
}
public function stop()
{
$command = 'kill ' . $this->pid;
exec($command);
if ($this->status() == false) return true;
else return false;
}
public function get_log_paths()
{
return "Log path: \nstdout: /tmp/out_" . $this->random . "\nstderr: /tmp/error_out_" . $this->random . "\n";
}
public function create_command_string()
{
$outPath = ' > /dev/null 2>&1';
if ($this->isOutPut) {
$this->random = rand(5, 15);
$outPath = ' 1> /tmp/out_' . $this->random . ' 2> /tmp/error_out_' . $this->random;
}
return 'nohup ' . $this->command . $outPath . ' & echo $!';
}
//do the process in background
public function do_process()
{
$command = $this->create_command_string();
exec($command, $process);
$this->pid = (int) $process[0];
}
/*
*To execute a PHP url on background you have to do the following things.
* $process=new BackgroundProcess("curl -s -o <Base Path>/log/xyz.log <PHP URL to execute> -d param_key=<Param_value>");
*/
}