--> Running shell script with python

Lutz Horn lutz.horn at posteo.de
Thu Aug 18 06:19:52 EDT 2016


> Tell me, do you know how can i send CTRl+C command from python to
> terminate this external shell script ?

os.system[1] is not an asynchronous function. It returns as soon as the 
called command terminates, not earlier.

If you want to execute a command in a subprocess, use 
subprocess.Popen[2]. You can then later kill this process using 
Popen.kill()[3].

 >>> import subprocess
 >>> p = suprocess.Popen(["sleep", "10]) # sleep 10 seconds
 >>> p.kill()

Lutz

[1] https://docs.python.org/3/library/os.html#os.system
[2] https://docs.python.org/3/library/subprocess.html#subprocess.Popen
[3] https://docs.python.org/3/library/subprocess.html#subprocess.Popen.kill



More information about the Python-list mailing list