简体   繁体   中英

How to open a subprocess (within a python script) in new terminal and kill it after 15 seconds (Platform: Ubuntu)

I am trying to start a subprocess in a new window from a python script file - timeout for 15 seconds then terminate that process without closing the main terminal window.

this is what I have so far...

cmd=subprocess.Popen('file_name.sh')
subprocess.Popen('gnome terminal -c cmd')
time.sleep(15)
Popen.terminate(cmd)

Try this:

import subprocess
import time

proc = subprocess.Popen(('gnome-terminal', '--', 'file_name.sh')) 
time.sleep(15)
subprocess.Popen(('pkill', '-f', 'file_name.sh'))

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