从python执行命令行

时间:2018-12-15 16:08:16

标签: python subprocess os.system

我想使用(os.system ...或subprocess模块)在系统中执行一些命令,并确保shell=False,所以我尝试了以下操作:

os.system('ssh -R 80:localhost:3000 serveo.net')

subprocess.call('ssh -R 80:localhost:3000 serveo.net', shell=True)

它可以工作,但是会挂起,直到过程完成为止,而我所不想要的是,我真的想执行命令并在执行命令时完成程序,所以我尝试了此操作:

subprocess.call('ssh -R 80:localhost:3000 serveo.net', shell=False)

但是,这引发了异常

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/local/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ssh -R 80:localhost:3000 serveo.net': 'ssh -R 80:localhost:3000 serveo.net'

我的问题是,如何在程序关闭时保持命令运行?

  • 注意,我已经在启动时运行了该程序(编辑/etc/rc.local文件)。

0 个答案:

没有答案