0

Python sends:

os.system("sudo openvpn openvpn.ovpn")

to terminal

how do I tell terminal to listen to my next command I'm trying to send

sys.exit()

but it doesn't work

This program is coded with tkinter and there is a button to choose the vpn, but i want to make a stop button as well.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I'm guessing you want to type your sudo password next? – smac89 Dec 06 '18 at 04:25
  • yea so after i type sudo password, the program will initialize the connection but while it is connecting i cant click anything on my gui, so i thought there should be a way for python to listen to another command while the terminal is still running – Ekkasit Smithipanon Dec 06 '18 at 04:31
  • Use the `subprocess` module – smac89 Dec 06 '18 at 04:37
  • 1
    Possible duplicate of [Not able to execute terminal command(top) in python](https://stackoverflow.com/questions/49766878/not-able-to-execute-terminal-commandtop-in-python) – smac89 Dec 06 '18 at 04:39
  • thank you! one more question if i use sys.exit() the program will terminate but i want only the terminal to stop how do I send a stop command? right now I'm using subprocess.Popen to send command to terminal – Ekkasit Smithipanon Dec 06 '18 at 04:59
  • You might be able to do something with the answers found [here](https://stackoverflow.com/questions/4789837/how-to-terminate-a-python-subprocess-launched-with-shell-true) – smac89 Dec 06 '18 at 05:06

2 Answers2

2

sys.exit will cause the current process exit. you can try POpen then use one one of

Popen.send_signal()
Popen.kill()
Popen.terminate()

to cause the subprocess quit. for example:

subp = subprocess.Popen("some command" )
subp.send_signal( 9 );
exudong
  • 366
  • 3
  • 13
  • what if right now i have Popen in one def function how do I pass on that value to another function, Popen.kill() requires self.These are under 2 click buttons; one to initiate the connection and the other is to kill the connection – Ekkasit Smithipanon Dec 06 '18 at 05:28
  • def clicked(): process = subprocess.Popen(['sudo','openvpn','--config',var.get()]) def clicked2(): process.kill() process is in another defined function so I cannot recall it, anyway i can recall that in another function? – Ekkasit Smithipanon Dec 06 '18 at 05:45
  • What are you trying to do in this comment? – Sean Pianka Dec 06 '18 at 05:46
0
def clicked():
    process = subprocess.Popen(['sudo','openvpn','--config',var.get()])

def clicked2():
    process.kill()

process is in another defined function so I cannot recall it, anyway i can recall that in another function?