簡體   English   中英

將Ctrl-C模擬為python腳本

[英]Simulate Ctrl-C to python script

我有一個python腳本,它等待一些作業並在線程中執行它們(使用subprocess.Popenshell=True )。 當我在外殼中運行腳本並嘗試使用Ctrl-C終止該腳本時,它將正常正常關閉。

問題是我想將此腳本作為守護程序運行,然后使用某種unix信號終止它。 INT信號應與Ctrl-C相同,但不能以相同的方式工作。 它使subproces.popen子進程繼續運行。

當我收到信號時,我還嘗試在主線程中提高KeyboardInterupt ,但這也無法關閉腳本並殺死所有子進程。

任何建議如何模擬Ctrl-C嗎?

調用subprocess.popen示例:

shell_cmd = "bwa aln -t 8 file1.fasta file1.fastq.gz > file1.sam.sai"
process = subprocess.Popen(shell_cmd,
                           shell=True,
                           stderr=subprocess.PIPE)

提高KeyboardInterrupt的主要工序提出了KeyboardInterrupt的主要工序。 您必須將信號發送到子流程。

您是否嘗試過Popen.send_signal

或者,更直接地說, Popen.terminate

Ctrl-C將SIGINT發送到整個進程組,而不僅僅是一個進程 使用os.killpg()或帶有kill的否定進程ID將SIGINT發送到進程組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM