LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2018, 07:27 AM   #1
benalex
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Rep: Reputation: Disabled
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 ?

Please help me.

Last edited by benalex; 02-27-2018 at 07:28 AM.
 
Old 02-28-2018, 12:22 AM   #2
Ztcoracat
LQ Guru
 
Registered: Dec 2011
Distribution: Slackware, Manjaro
Posts: 8,784
Blog Entries: 14

Rep: Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069Reputation: 1069
Hi & Welcome to LQ.-

If you just want to kill the python interpeter try here:

https://stackoverflow.com/questions/...m-the-terminal

HTH
 
Old 02-28-2018, 01:41 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,558

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
@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.
 
1 members found this post helpful.
Old 02-28-2018, 11:24 AM   #4
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,975

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Run process for 10 seconds and quit.

Code:
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()

Last edited by teckk; 02-28-2018 at 11:26 AM.
 
1 members found this post helpful.
Old 03-01-2018, 10:32 AM   #5
benalex
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Run process for 10 seconds and quit.

Code:
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 ?

-ben
 
Old 03-01-2018, 12:38 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,558

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Well, I mentioned 'popen'. Have you tried to use it in your program?
 
Old 03-01-2018, 12:47 PM   #7
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,975

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
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)

Last edited by teckk; 03-01-2018 at 01:02 PM.
 
1 members found this post helpful.
Old 03-02-2018, 11:15 AM   #8
benalex
LQ Newbie
 
Registered: Feb 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
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.

How to kill this process ?

-ben
 
Old 03-02-2018, 12:41 PM   #9
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,975

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Open up top or htop and look at what you are doing.
Code:
p1 = subprocess.Popen('emacs &', shell=True)
You spawned another shell.

Code:
import os, signal, subprocess, time

a = subprocess.Popen('xterm &', shell=True, preexec_fn=os.setsid)

time.sleep(5)
                       
os.killpg(os.getpgid(a.pid), signal.SIGTERM)
 
Old 03-02-2018, 01:06 PM   #10
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 1,975

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
While in python shell
Code:
>>>import os
>>>import signal
>>>import subprocess
>>>help(os)
>>>help(signal)
>>>help(subprocess)
Use the / to search. n and shift+n for next-last.
 
Old 03-02-2018, 08:40 PM   #11
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 579

Rep: Reputation: 267Reputation: 267Reputation: 267
Quote:
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
What is this utility?

This may be useful, the question states the linux version works fine
https://stackoverflow.com/questions/...cts-on-windows

Last edited by Sefyir; 03-02-2018 at 08:43 PM.
 
Old 03-06-2018, 01:48 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,558

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
ping(8)
 
Old 03-06-2018, 02:56 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,282

Rep: Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390
you must not use & with popen, just
Code:
p1 = subprocess.Popen('emacs', shell=False)
subprocess.Popen.kill(p1)
& 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)
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 07:51 AM
Alternative to kill system call using c program to test only process. deymrinmoy Linux - Kernel 3 01-16-2013 10:47 PM
Kill a zombie process - which process is the parent that I should kill? Mountain Linux - General 3 12-31-2011 02:44 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
If I want, that process has automatically started at system loading or reloading ukrainet Linux - Newbie 2 12-14-2004 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration