一、使用subprocess模塊
使用subprocess模塊可創建子進程。
subprocess. Popen ( args , bufsize=0 , executable=None , stdin=None , stdout=None , stderr=None , preexec_fn=None , close_fds=False , shell=False , cwd=None , env=None , universal_newlines=False , startupinfo=None , creationflags=0 )
subprocess.Popen(['google-chrome',"http://www.baidu.com"])可打開chrome並顯示百度網頁。
subprocess.Popen.pid可顯示子進程的id,但這只是打開的shell的id,如果此時使用pid.kill是無法殺掉所有子進程的。
殺掉所有子進程,代碼:
import sys, os, subprocess, time, signal p = subprocess.Popen(['google-chrome',"http://www.baidu.com"], close_fds=True, preexec_fn = os.setsid) print p.pid time.sleep(5) # p.kill() #無法殺掉所有子進程,只能殺掉子shell的進程 # p.terminate() #無法殺掉所有子進程 os.killpg( p.pid,signal.SIGUSR1) time.sleep(3)
注意:
close_fds=True,此時除了文件描述符為0 , 1 and 2,其他子進程都要被殺掉。( Linux中所有的進程都是進程0的子進程。
pid=1的是init,內核完成之后啟動的第一個進程,然后init根據/etc/inittab的內容再去啟動其它進程。)
os.setsid(): 使獨立於終端的進程(不響應sigint,sighup等),使脫離終端。
SIGUSR1: 用戶自定義信號
os.killpg( p.pid,signal.SIGUSR1): 發送SIGUSR1信號到組進程p.pid
二、封裝subprocess 函數
最近使用python 調用ansible命令,來監測系統相關服務。發現到達指定時間,ansible進程沒有殺掉。導致服務器卡死。
后來嘗試 使用os.killpg ,發現可以殺掉ansible 子進程。
完整代碼如下:
#!/usr/bin/env python3 # coding: utf-8 import os import time import signal import subprocess def execute_linux2(cmd, timeout=10, skip=False): """ 執行linux命令,返回list :param cmd: linux命令 :param timeout: 超時時間,生產環境, 特別卡, 因此要3秒 :param skip: 是否跳過超時限制 :return: list """ p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid) t_beginning = time.time() # 開始時間 while True: if p.poll() is not None: break seconds_passed = time.time() - t_beginning if not skip: if seconds_passed > timeout: # p.terminate() # p.kill() # raise TimeoutError(cmd, timeout) print('錯誤, 命令: {},本地執行超時!'.format(cmd)) # 當shell=True時,只有os.killpg才能kill子進程 try: # time.sleep(1) os.killpg(p.pid, signal.SIGUSR1) except Exception as e: pass return False result = p.stdout.readlines() # 結果輸出列表 return result
本文參考鏈接:
https://blog.csdn.net/heihuifeng/article/details/6543414