-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellCmd.py
More file actions
71 lines (57 loc) · 2.02 KB
/
shellCmd.py
File metadata and controls
71 lines (57 loc) · 2.02 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
#!/bin/env python
# -*- coding:utf-8 -*-
import subprocess
import time
import sys
import os
import signal
def shell_cmd(cmd, timeout=None, show=True, stdin_file=None):
'''
Will execute a command, read the output and return it back.
@param cmd: command to execute
@param timeout: process timeout in seconds
@param show: Display output to the front
@param stdin_file: Enter the interactive information content of the file
@return: a tuple of three: first stdout, then stderr, then exit code
@raise OSError: on missing command or if a timeout was reached
'''
ph_out = None # process output
ph_err = None # stderr
ph_ret = None # return code
if show is True:
if stdin_file is not None:
_stdin = open(stdin_file)
else:
_stdin = sys.stdin
_stdout = sys.stdout
_stderr = sys.stderr
else:
if stdin_file is not None:
_stdin = open(stdin_file)
else:
_stdin = subprocess.PIPE
_stdout = _stderr = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True,
stdout=_stdout,
stdin=_stdin,
stderr=_stderr)
# if timeout is not set wait for process to complete
if not timeout:
ph_ret = p.wait()
else:
fin_time = time.time() + timeout
while p.poll() == None and fin_time > time.time():
time.sleep(1)
# if timeout reached, raise an exception
if fin_time < time.time():
# starting 2.6 subprocess has a kill() method which is preferable
p.kill()
#os.kill(p.pid, signal.SIGKILL)
raise OSError("Process timeout has been reached")
ph_ret = p.returncode
ph_out, ph_err = p.communicate()
return (ph_out, ph_err, ph_ret)
if __name__ == "__main__":
#print shell_cmd("pwd", show=True)
shell_cmd("python test.py", show=True, stdin_file="b.txt")
print shell_cmd("python test.py", show=False, stdin_file="b.txt")