How to kill a process started using os.system() in python3
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to kill a process started using os.system() in python3
Hi All,
I'm using python3 and has written code which uses
os.system() to launch a command line utility on Linux Ubuntu 14.0 PC.
Actually if I run this utility on Ubuntu command line and I want to stop running, it says press Ctrl + C. By pressing Ctrl+C the utility stops and command prompt will be available.
Now, after launching this utility from python code and I want to stop it after running for 5 minutes, how do I kill this process ?
@OP: You cannot, os.system won't return to you until the external program has terminated.
Maybe you could use subprocess.popen to get more control over the child-process.
import time, multiprocessing
def test():
for i in range(1, 1000000):
print (i) #Simulated process
time.sleep(.2)
a = multiprocessing.Process(target=test, name="Test")
a.start()
time.sleep(10)
a.terminate()
a.join()
import time, multiprocessing
def test():
for i in range(1, 1000000):
print (i) #Simulated process
time.sleep(.2)
a = multiprocessing.Process(target=test, name="Test")
a.start()
time.sleep(10)
a.terminate()
a.join()
Hi,
Actually I want to invoke a linux command line utility and to kill that I have to send a Ctrl + C command. How can I accomplish this in python using multiprocess or using subprocess ?
That was an example @benalex. Make a little effort.
Code:
#subprocess.Popen for 5 seconds, then quit
import time, subprocess
a = subprocess.Popen("exec " + '$TERM', stdout=subprocess.PIPE, shell=True)
time.sleep(5)
subprocess.Popen.kill(a)
Edit:
Actually that could be shortened.
Another example.
Code:
#subprocess.Popen multiple processes for x seconds, then quit
import time, subprocess
a = subprocess.Popen('xterm -bg red', shell=True)
b = subprocess.Popen('xterm -bg blue', shell=True)
c = subprocess.Popen('xterm -bg green', shell=True)
time.sleep(5)
subprocess.Popen.kill(a)
time.sleep(2)
subprocess.Popen.kill(b)
time.sleep(2)
subprocess.Popen.kill(c)
That was an example @benalex. Make a little effort.
Code:
#subprocess.Popen for 5 seconds, then quit
import time, subprocess
a = subprocess.Popen("exec " + '$TERM', stdout=subprocess.PIPE, shell=True)
time.sleep(5)
subprocess.Popen.kill(a)
Edit:
Actually that could be shortened.
Another example.
Code:
#subprocess.Popen multiple processes for x seconds, then quit
import time, subprocess
a = subprocess.Popen('xterm -bg red', shell=True)
b = subprocess.Popen('xterm -bg blue', shell=True)
c = subprocess.Popen('xterm -bg green', shell=True)
time.sleep(5)
subprocess.Popen.kill(a)
time.sleep(2)
subprocess.Popen.kill(b)
time.sleep(2)
subprocess.Popen.kill(c)
Hi,
I tried all but after that when I checked "ps -aef" I could see the task is
alive in background. So I added sudo killall <myprocess> in my python code.
This killed the utility which I invoked using subprocess.Popen()
I tried opening emacs using p1 = subprocess.Popen('emacs &', shell=True)
After that if i kill using subprocess.Popen.kill(p1), it doesn't kill the
process.
& will produce an additional process (shell=True too), therefore you will not (be able to) find the process itself easily (and therefore you will kill probably its parent)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.