luki
luki

Reputation: 309

kill process do not kill the subprocess and do not close a terminal window

I am working on UBUNTU and I have file main.py with a code inside:

#!/usr/bin/env python3
# coding=utf-8
import os
import time
from subprocess import Popen, PIPE, call, signal

base_path = os.path.abspath('')
path_to_file = base_path + '/test_subprocess.py'
p = Popen(['gnome-terminal', "--",  path_to_file])
time.sleep(2)

os.kill(p.pid, signal.SIGKILL)

I have test_subprocess.py with code like that:

#!/usr/bin/env python3
# coding=utf-8

import time

def print_message():
    while True:
        print('I am working!')
        time.sleep(0.5)
    
print_message()

I tried to kill the subprocess but after

os.kill(p.pid, signal.SIGKILL)

subprocess is still working and prints 'I am working!'

How can I finish subprocess and how to close gnome terminal? If I selected completely wrong way. Can you show me working example?

New version of test_subprocess.py

#!/usr/bin/env python3
# coding=utf-8
import sys
from subprocess import signal
import time

def print_message():
    while True:
        print('I am working!')
        time.sleep(0.5)
        if signal.SIGKILL:  # it is braking a loop when parent process terminate!
            print('I am killing self!')
            break

print_message()

Should I do it like above?

Upvotes: 4

Views: 1962

Answers (2)

You could try the following:

p = Popen(['gnome-terminal', "--",  path_to_file])
PIDs = p.pid
os.system("kill {0}".format(PIDs))

Popen.pid The process ID of the child process.

Note that if you set the shell argument to True, this is the process ID of the spawned shell.

http://docs.python.org/library/subprocess.html

This will at least kill the correct process. Not sure if it will close the terminal.

Edit: to kill the process and close the terminal:

p = Popen(['gnome-terminal', '--disable-factory', '-e', path_to_file], preexec_fn=os.setpgrp)

os.killpg(p.pid, signal.SIGINT)

Credit to https://stackoverflow.com/a/34690644/15793575, whih I modified for your command:

  • --disable-factory is used to avoid re-using an active terminal so that we can kill newly created terminal via the subprocess handle
  • os.setpgrp puts gnome-terminal in its own process group so that os.killpg() could be used to send signal to this group

Upvotes: 1

TheEagle
TheEagle

Reputation: 5992

Popen.pid
The process ID of the child process.

Note that if you set the shell argument to True, this is the process ID of the spawned shell.

Try setting the shell argument of the Popen constructor to False. (p = Popen(['gnome-terminal', "--", path_to_file]) -> p = Popen(['gnome-terminal', "--", path_to_file], shell=False)). I had a similar issue not long ago - this fixed it for me.

Upvotes: 0

Related Questions