运行shell命令
项目描述
安装
$ [sudo] pip install runcmd
示例
>>> import runcmd
>>> r = runcmd.run(["echo", "hello world"])
>>> r.code # exit status code
0
>>> r.out # stdout
'hello world'
>>> r.err # stderr
''
>>> r.pid # process pid
1234
background=True
>>> r = runcmd.run(["sleep","5"],background=True)
>>> while r.running: # True if process is running and not "zombie process"
>>> print("running")
kill(signal=None)
- 终止进程
>>> r.kill(-9)
exc()
- 如果代码不是 0
则引发异常
>>> runcmd.run(["ls"]).exc() # code 0, ok
>>> runcmd.run(["mkdir", "/"]).exc() # code 1, raise OSError
...
OSError: exited with code 1
mkdir: /: Is a directory