启动和停止外部程序?

2024-04-24 22:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python3的Linux上启动和停止一个外部程序。 这个程序是一个名为smip-tun,的可执行文件,与源代码位于同一个文件夹中,我希望能够从那里用相对的名称来执行和控制它。你知道吗

不幸的是,这不起作用:

smip_tun = 0

def start_smip_tun(self):
    smip_tun =  subprocess.Popen(['smip-tun',DEFAULT_SERIAL_PORT])

def end_smip_tun(self):
    smip_tun.terminate()
    print("end")

但是说它找不到smip-tun。指定相对目录的操作失败。我试着用cwd但是弄不明白。
有什么建议吗?你知道吗

编辑:

使Smip tun全球化,但问题仍然存在。你知道吗

新代码:

smip_tun = 0

def start_smip_tun(self):
    global smip_tun
    smip_tun =  subprocess.Popen(['smip-tun',DEFAULT_SERIAL_PORT])

def end_smip_tun(self):
    global smip_tun
    smip_tun.terminate()
    print("end")

错误消息:

  File "/home/sven/git/cerberos_manager/iot_network_api.py", line 40, in start_smip_tun
    smip_tun = subprocess.Popen(['smip-tun',DEFAULT_SERIAL_PORT])
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'smip-tun'

Tags: inpyselfdefaultportdeflineserial
1条回答
网友
1楼 · 发布于 2024-04-24 22:28:13
import os

os.chdir('.') # or change the . for the full path directory.

smip_tun =  subprocess.Popen(['smip_tun',DEFAULT_SERIAL_PORT])

还要确保你的应用没有任何扩展,比如smip-tun.pysmip-tun.sh

相关问题 更多 >