This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
signal_console.php
executable file
·220 lines (176 loc) · 6.21 KB
/
signal_console.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/php
<?php
/**
* A simple console that lets you send signals to a process
* Set your own pid file locations in the array below so you can read the PIDs from them by passing in their array ordinal.
* @example To attach the signal console to the pidfile listed 2nd in the array below, you'd type -1 at the signal prompt.
* The dash indicates a shortcut (instead of attaching to PID 1) and "1" is the array ordinal for the 2nd item.
*
* User: Shane Harter
* Date: 4/24/12
*/
define('BASE_PATH', dirname(dirname(__FILE__)));
$pidfiles = array(
'PrimeNumbers' => BASE_PATH . '/Examples/PrimeNumbers/pid',
'Tasks' => BASE_PATH . '/Examples/Tasks/pid',
'LongPoll' => BASE_PATH . '/Examples/LongPoll/pid',
);
$index = array_keys($pidfiles);
$string = '';
foreach($index as $id => $key) {
$string .= sprintf('%s [-%s] %s', PHP_EOL, $id, $key);
}
echo PHP_EOL, "PHP Simple Daemon - Signal Console";
echo PHP_EOL, "Enter PID";
if ($string)
echo " or Select a Daemon:", $string;
echo PHP_EOL;
echo PHP_EOL;
stream_set_blocking(STDIN, 0);
$pid = false;
$input = '';
$macro_input = '';
$prompt = true;
$flash = '';
while(true) {
$flash = '';
// Every few iterations verify that the pid still exists
if ($pid && ($input || mt_rand(1,3) == 2) && (file_exists("/proc") && !file_exists("/proc/{$pid}"))) {
out("Process {$pid} Has Exited");
$prompt = true;
$pid = false;
if (!$input)
echo PHP_EOL;
}
if ($input || $prompt)
if (!$pid)
echo "PID > ";
else
echo "SIG > ";
$prompt = false;
if ($macro_input) {
$input = $macro_input;
$macro_input = '';
} else {
$input = strtolower(fgets(STDIN));
}
$prompt = $input == "\n";
$input = trim($input);
try {
switch(true) {
case empty($input):
continue;
case input() == 'exit':
exit;
case $pid && input() == 'help':
$out = array();
$out[] = '';
$out[] = 'Available Commands:';
$out[] = 'exit';
$out[] = 'help Display this help';
$out[] = 'pid [integer] Display a PID prompt to enter a new PID, and optionally pass a PID to switch directly to';
$out[] = '[integer] A valid signal';
$out[] = '';
out(implode(PHP_EOL, $out));
continue;
case input() == 'help':
$out = array();
$out[] = '';
$out[] = 'Available Commands:';
$out[] = 'exit';
$out[] = 'help Display this help';
$out[] = '[integer] A valid process id';
$out[] = '';
$out[] = 'Shortcuts:';
foreach($index as $i => $f) {
$out[] = str_pad("-$i", 20, ' ', STR_PAD_RIGHT) . $pidfiles[$f];
}
$out[] = '';
$out[] = 'Shortcuts come from the $pidfiles array. Add your own shortcuts to make life easier.';
$out[] = '';
out(implode(PHP_EOL, $out));
continue;
case $pid && is_numeric($input) && $input > 0 && $input < 50:
out("Sending Signal...");
posix_kill($pid, $input);
continue;
case input(substr($input, 0, 3)) == 'pid':
if ($pid) {
out("Releasing PID...");
$pid = false;
}
macro(1, $input);
continue;
case $input <= 0 && isset($index[abs($input)]):
$key = $index[abs($input)];
$file = $pidfiles[$key];
if (!file_exists($file))
throw new Exception("Sorry, Pid File Not Found! Tried: $file");
$pid = file_get_contents($file);
out("Setting PID From Shortcut: {$pid}");
continue;
case !$pid && (is_numeric($input) && $input < 40000 && $input > 1):
// On linux systems, easily verify if the pid is valid by looking for the process in the /proc directory
if (file_exists("/proc") && !file_exists("/proc/$input"))
throw new Exception("Pid `$input` Does Not Exist");
out("Setting PID: {$input}");
$pid = $input;
continue;
case $pid:
throw new Exception("Invalid Signal");
default:
throw new Exception('Invalid Input: Valid PID or Shortcut Required');
}
}
catch(Exception $e)
{
echo $e->getMessage(), PHP_EOL, PHP_EOL;
}
usleep(20 * 1000);
}
function out($out)
{
global $flash;
if ($flash) {
echo $flash, PHP_EOL;
$flash = '';
}
echo $out, PHP_EOL;
}
function macro($id) {
global $input, $macro_input, $prompt, $address;
if ($macro_input)
$macro_input = '';
switch($id){
case 1:
// Macro 1 reads an optional PID from a pid-reset command.
// Example: SIG> pid 12345 The current pid will be released and this macro will pull 12345 out and set
// it as the new pid.
$arg = @func_get_arg(1);
if (!$arg || strlen(trim($arg)) < 4)
return;
$macro_input = trim(str_replace('pid', '', $arg));
break;
}
if ($macro_input)
$input = '';
}
function input($in = null, Array $commands = array('pid', 'help', 'exit')) {
global $input, $flash;
if (empty($in))
$in = $input;
if (empty($in))
return null;
$matches = array();
foreach ($commands as $command)
if ($in == substr($command, 0, strlen($in)))
$matches[] = $command;
if (count($matches) > 1)
out("Ambiguous Command. Matches: " . implode(', ', $matches));
if (count($matches)) {
if ($in != $matches[0])
$flash = $matches[0];
return $matches[0];
}
return null;
}