How to determine pid of process started via os.system
os.system
return exit code. It does not provide pid of the child process.
Use subprocess
module.
import subprocess
import time
argument = '...'
proc = subprocess.Popen(['python', 'bar.py', argument], shell=True)
time.sleep(3) # <-- There's no time.wait, but time.sleep.
pid = proc.pid # <--- access `pid` attribute to get the pid of the child process.
To terminate the process, you can use terminate
method or kill
. (No need to use external kill
program)
proc.terminate()
Terminate Python Process and halt it's underlying os.system command line
You can use taskkill
but you have to use the /T
(and maybe /F
) switch so all child processes of the cmd process are killed too. You get the process id of the cmd task via process.pid
.
os.system can start but cannot kill a Python process
Can you use os.kill()
instead?
import os
import signal
os.kill(pid, signal.SIGTERM)
If you don't have the pid
readily available, use subprocess.check_output
to acquire it. (Use subprocess.run
instead if you're using Python 3.5+.)
import os
import signal
import subprocess
def get_water_pid():
return int(subprocess.check_output(['pgrep', '-f', 'water.py']).strip())
def kill_water_process():
pid = get_water_pid()
os.kill(pid, signal.SIGTERM)
run os.system command as subprocess python
.split
is not doing you any favors here, because you have spaces within quotes that you don't want to split on. Either pass a string directly, and let the shell handle argument separation:
cmd = "/usr/atria/bin/cleartool setview -exec '/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py' testUser__project_5_0_myProject_001"
p=subprocess.Popen(cmd, shell=True)
Or separate the command manually:
cmd = [
"/usr/atria/bin/cleartool",
"setview",
"-exec",
# single quotes in the argument no longer required, as we're not going through the shell
"/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py",
"testUser__project_5_0_myProject_001"
]
p = subprocess.Popen(cmd)
How to kill a process in window without killing current process
Nice try @Pryanshu
You're doing it right. The problem with your current approach is python and the batch script will be under same process and get's killed at the same time.
So just change the approach a bit
here's the gist:
python
get current process id
while loop
read file
if matches condition
call kill.bat and pass this python process ID as separate process
batch
get first parameter (which is the python process to kill)
kill the process via taskkill
start the python script as seprate process
exit
Helpers
get current process ID - pid = os.getpid()
pass id to batch script - os.system("kill.bat %s" % pid)
batch script get arguments - %1
this %1 will contain the first argument passed.
by default %* will contain all the arguments passed to the script.
python start program as
separate process - use subprocess python package
cmd start program as
separate process - use Start command
kill process via taskkill - taskkill /F /PID <pid>
replace <pid> with your argument
I know you can handle the code part. You'll have to make use of the Helpers and combine them to do the task. Lemme know the output.
If you have any question do comment. I'll try to reply ASAP
Cheers
Related Topics
Setting Ld_Library_Path from Inside Python
Can You Fool Isatty and Log Stdout and Stderr Separately
Gae " No Attribute 'Httpshandler' " Dev_Appserver.Py
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command
How to Make Python3 Command Run Python 3.6 Instead of 3.5
Python: When to Use Pty.Fork() Versus Os.Fork()
Need a Way to Determine If a File Is Done Being Written To
Running Simulation with Hyperthreading Doubles Runtime
How to Limit the Amount of Time a Function Can Run for (Add a Timeout)
Python 2.7 Cx_Freeze: Importerror: No Module Named _Startup_
Signal Handling in Multi-Threaded Python
Error with Igraph Library - Deprecated Library
How to Make the Python Program to Check Linux Services
Python Sigkill Catching Strategies
Capturing Output of Python Script Run Inside a Docker Container
Pairing Bluetooth Devices with Passkey/Password in Python - Rfcomm (Linux)