user3493202
user3493202

Reputation:

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'

Upvotes: 0

Views: 250

Answers (1)

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, like smip-tun.py and smip-tun.sh

Upvotes: 1

Related Questions