Sven Sven - 6 days ago 6
Linux Question

Start and stopping external program?

I want to start and stop an external program in Python3, on Linux.
This program is an executable, called

smip-tun,
located in the same folder as the source code and I'd like to be able to execute and control it from there with relative names.

Unfortunately, this does not work:

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")


But says it cannot find
smip-tun
. Efforts to specify the relative directory have failed. I was trying with
cwd
but cannot figure it out.

Any advice?

Edit:

Made Smip-tun global but the issue persists.

New code:

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")


Error message:

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'

Answer

import os

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

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

Also make sure your app do not have any extension(ex: smip-tun.py, smip-tun.sh...)

Comments