-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipe-demo.php
executable file
·132 lines (122 loc) · 2.8 KB
/
pipe-demo.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
#!/usr/bin/php5
<?php /*
### BEGIN INIT INFO
# Provides: pipe-demo
# Required-Start: $pipe-server $local_fs $remote_fs $syslog $network
# Required-Stop: $local_fs $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Daemon for running packet listeners
# Description: Daemon for running packet listeners
### END INIT INFO
*/
/**
* Config
*/
define('WORKING_DIR', __DIR__ . '/');
include WORKING_DIR . 'shell-common.php';
/**
* Performs installation of service
*/
function doInstall()
{
if (!isLinux()) {
print "Install is not supported on windows!";
die();
}
shell_exec('sudo ln -s ' . __FILE__ . ' /etc/init.d/pipe-demo');
shell_exec('sudo chmod +x ' . __FILE__);
shell_exec('sudo update-rc.d pipe-demo defaults');
print "Installation complete\n";
}
/**
* Service start
*/
function serviceStart($debug)
{
print "Starting demo-server... ";
if ($debug) {
$params = '-d navitech -p conf/pipe-debug.conf';
} else {
$params = '';
}
if (isLinux()) {
exec("sudo -u pipe python3 demo.py $params >/dev/null &");
} else {
$shell = new COM("WScript.Shell");
$shell->run("python3 demo.py $params", 0, false);
}
print "[OK]\n";
}
/**
* Kills pipe-server processes
*/
function serviceStop($debug)
{
if ($debug) {
$mask = "[p]ython.*demo\.py.*debug";
} else {
$mask = "[p]ython.*demo\.py\s*$";
}
print "Stopping... ";
$command = "sudo pkill -f $mask 2>&1";
if (!isLinux()) {
$command = "wmic Path win32_process Where " .
"\"CommandLine Like '%python% demo.py%'\" Call Terminate";
}
$output = shell_exec($command);
print_r($output);
print "[OK]\n";
}
/**
* Tests if process already running
*/
function serviceTest()
{
if (!isLinux()) { return; }
$mask = "[p]ython.*demo\.py";
$output = shell_exec("sudo pgrep -f $mask 2>&1");
if ($output != NULL) {
print "Democlient is running\n";
} else {
print "Democlient is down\n";
}
}
print "Pipe-demo Starter v2.0.0\n";
// read input arguments
$command = '';
$params = arguments($argv);
if (is_array($params['input']) && !empty($params['input'][0])) {
$command = array_shift($params['input']);
}
if (is_array($params['input']) && !empty($params['input'][0])) {
$debug = ($params['input'][0] == 'debug');
} else {
$debug = false;
}
switch ($command)
{
case 'install':
doInstall();
break;
case 'start':
serviceStart($debug);
break;
case 'stop':
serviceStop($debug);
break;
case 'restart':
case 'force-reload':
case 'reload':
serviceStop($debug);
serviceStart($debug);
break;
case 'status':
serviceTest($debug);
break;
default:
$file = basename(__FILE__, '.php');
print "Usage: service $file {start|stop|restart|reload|force-reload|status} {debug|normal}\n" .
"Install: php " . $params['name'] . " install\n";
}