-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
executable file
·45 lines (41 loc) · 1.41 KB
/
Copy pathexecutor.py
File metadata and controls
executable file
·45 lines (41 loc) · 1.41 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
import subprocess
import threading
import signal
import os
""" Run system commands with timeout
"""
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
self.out = None
def run_command(self, capture = False):
if not capture:
self.process = subprocess.Popen(self.cmd,shell=True)
self.process.communicate()
return
# capturing the outputs of shell commands
self.process = subprocess.Popen(self.cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
out,err = self.process.communicate()
if len(out) > 0:
self.out = out.splitlines()
else:
self.out = None
# set default timeout to 2 minutes
def run(self, capture = False, timeout = 10):
thread = threading.Thread(target=self.run_command, args=(capture,))
thread.start()
thread.join(timeout)
if thread.is_alive():
print('Command timeout, kill it: ' + self.cmd)
self.process.terminate()
print("Kiiiillled")
self.process.kill()
print("Kiiiillled2")
print(self.process.pid)
os.kill(self.process.pid, signal.SIGINT)
os.kill(self.process.pid+1, signal.SIGINT)
thread.join()
print("Kiiiillled3")
return False
return self.out