Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to kill a pid [SOLVED]
#5
Well, the wav file is hard to kill. It only dies with CTR-C which I cannot use.
Your code returns the python pid and not the mplayer's.

This' the modified code:

import multiprocessing
import os
import psutil
import time
from subprocess import check_output

file = 'music.wav'

def killtree(pid, including_parent=True):
    parent = psutil.Process(pid)
    for child in parent.children(recursive=True):
        print("child", child)
        child.kill()
    if including_parent:
        parent.kill()  ## this program

def song():
    command = 'mplayer -softvol -volume 1 %s 1>/dev/null 2>&1' % file
    os.system(command)


pid=os.getpid() ##this sketch's pid!!

mp = multiprocessing.Process(target=song)
mp.start()

p= psutil.Process(mp.pid)
print 'pid =', pid, p
print "status", mp.is_alive()
 
time.sleep(0.5)
if mp.is_alive():
     print("terminate")
     mp.terminate()
     mp.join()
else:
    print "already terminated"
    print 'pid =', pid, p
    print "status", mp.is_alive()

killtree(pid)
Reply


Messages In This Thread
How to kill a pid [SOLVED] - by ebolisa - Oct-26-2018, 09:36 PM
RE: How to kill a pid - by woooee - Oct-26-2018, 10:17 PM
RE: How to kill a pid - by ebolisa - Oct-27-2018, 09:14 AM
RE: How to kill a pid - by woooee - Oct-27-2018, 08:04 PM
RE: How to kill a pid - by ebolisa - Oct-27-2018, 10:59 PM
RE: How to kill a pid - by woooee - Oct-27-2018, 11:18 PM
RE: How to kill a pid [SOLVED] - by ebolisa - Oct-28-2018, 01:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 275 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,606 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Using a button to kill and restart a script duckredbeard 3 3,382 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  how to check for thread kill flag nanok66 1 2,224 May-09-2020, 10:06 PM
Last Post: nanok66
  kill thread or process asap, even during time.sleep nanok66 4 2,986 Apr-29-2020, 10:13 AM
Last Post: nanok66
  Linux: Automatically kill process with fixed exe path anddontyoucomebacknomore 4 3,098 Apr-22-2019, 07:35 AM
Last Post: anddontyoucomebacknomore

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020