简体   繁体   中英

kill matlab process in task manager using python

Every now and then, I run into this problem: my python script calls matlab, runs a simulation and closes matlab. Sometimes, matlab does not close properly. then, i run batch script that builds Visual studio dll/lib , which unfortunately are held up by matlab.

Is there a way to find process id for Matlab using python and then kill it.
I can then add this to end of my python script to safely close matlab.
Thanks
sedy

To kill all processes containing "matlab" in its name you can use

import os
import psutil

allMatlabIds = [p.pid for p in psutil.process_iter() if "matlab" in str(p.name)]
MatlabIdsToKill = [x for x in allMatlabIds if x != os.getpid()]
for MatlabId in MatlabIdsToKill:
    os.kill(MatlabId, signal.SIGINT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM