如何抓住Cntrl+;运行Python脚本的shell脚本中的C

如何抓住Cntrl+;运行Python脚本的shell脚本中的C,python,shell,Python,Shell,我试图编写一个简单的程序,运行Python程序并检查生成的输出文件: #!/bin/bash rm archived_sensor_data.json python rethinkdb_monitor_batch.py trap "gedit archived_sensor_data.json" 2 Python脚本referencedb\u monitor\u batch.py无限期运行,并将(以仅附加模式)写入文件archived\u sensor\u data.json。为了每次都能从

我试图编写一个简单的程序,运行Python程序并检查生成的输出文件:

#!/bin/bash

rm archived_sensor_data.json
python rethinkdb_monitor_batch.py
trap "gedit archived_sensor_data.json" 2
Python脚本
referencedb\u monitor\u batch.py
无限期运行,并将(以仅附加模式)写入文件
archived\u sensor\u data.json
。为了每次都能从头开始,我希望每次运行之前都删除该文件。然后,在使用Cntrl+C中断执行之后,我想使用Gedit自动触发文件的打开

问题是,当我按下Cntrl+C时,它似乎不会自动打开Gedit。
2
不是此处使用的正确退出代码吗

import signal import os os.system("rm archived_sensor_data.json") def signal_handler(signal, frame): print('You pressed Ctrl+C!') os.system("gedit archived_sensor_data.json") signal.signal(signal.SIGINT, signal_handler) #your remaining code #must be placed here 输入信号 导入操作系统 操作系统(“rm存档的传感器数据.json”) def信号处理器(信号,帧): 打印('按Ctrl+C!') 系统(“gedit存档的传感器数据.json”) signal.signal(signal.SIGINT,信号处理器) #您剩余的代码 #必须放在这里

只需使用以下命令运行代码

$db\u monitor\u batch.py

这一定能解决你的问题
有关详细信息,请阅读信号处理程序

输入信号 导入操作系统 操作系统(“rm存档的传感器数据.json”) def信号处理器(信号,帧): 打印('按Ctrl+C!') 系统(“gedit存档的传感器数据.json”) signal.signal(signal.SIGINT,信号处理器) #您剩余的代码 #必须放在这里

只需使用以下命令运行代码

$db\u monitor\u batch.py

这一定能解决你的问题

阅读信号处理程序了解更多信息

您可以通过在
重新思考db\u monitor\u batch.py
中捕获信号来执行此操作,如下所示:

#!/usr/env/bin python

try:
    # your existing code here---let's assume it does the following:

    import time
    outfile = open( "archived_sensor_data.json", "wt" )  # NB: this already does the job of erasing previous content
    while True:
        outfile.write( "There's a horse in aisle five.\n" )
        time.sleep( 1 )
        outfile.write( "My house is full of traps.\n" )
        time.sleep( 1 )

except KeyboardInterrupt:
    print( "You pressed Ctrl-C" )
except KeyboardInterrupt:
    os.execlp("gedit", "archived_sensor_data.json")
…那么包装器脚本就是:

#!/bin/bash

python rethinkdb_monitor_batch.py
gedit archived_sensor_data.json
但实际上,既然可以在Python中完成所有工作,那么为什么还要费心处理包装器呢?将最后的
print()
调用替换为如下所示:

#!/usr/env/bin python

try:
    # your existing code here---let's assume it does the following:

    import time
    outfile = open( "archived_sensor_data.json", "wt" )  # NB: this already does the job of erasing previous content
    while True:
        outfile.write( "There's a horse in aisle five.\n" )
        time.sleep( 1 )
        outfile.write( "My house is full of traps.\n" )
        time.sleep( 1 )

except KeyboardInterrupt:
    print( "You pressed Ctrl-C" )
except KeyboardInterrupt:
    os.execlp("gedit", "archived_sensor_data.json")

…然后直接从命令行调用Python脚本。

您可以通过捕获
rewritedb\u monitor\u batch.py
中的信号来完成此操作,如下所示:

#!/usr/env/bin python

try:
    # your existing code here---let's assume it does the following:

    import time
    outfile = open( "archived_sensor_data.json", "wt" )  # NB: this already does the job of erasing previous content
    while True:
        outfile.write( "There's a horse in aisle five.\n" )
        time.sleep( 1 )
        outfile.write( "My house is full of traps.\n" )
        time.sleep( 1 )

except KeyboardInterrupt:
    print( "You pressed Ctrl-C" )
except KeyboardInterrupt:
    os.execlp("gedit", "archived_sensor_data.json")
…那么包装器脚本就是:

#!/bin/bash

python rethinkdb_monitor_batch.py
gedit archived_sensor_data.json
但实际上,既然可以在Python中完成所有工作,那么为什么还要费心处理包装器呢?将最后的
print()
调用替换为如下所示:

#!/usr/env/bin python

try:
    # your existing code here---let's assume it does the following:

    import time
    outfile = open( "archived_sensor_data.json", "wt" )  # NB: this already does the job of erasing previous content
    while True:
        outfile.write( "There's a horse in aisle five.\n" )
        time.sleep( 1 )
        outfile.write( "My house is full of traps.\n" )
        time.sleep( 1 )

except KeyboardInterrupt:
    print( "You pressed Ctrl-C" )
except KeyboardInterrupt:
    os.execlp("gedit", "archived_sensor_data.json")

…然后直接从命令行调用Python脚本。

其他答案涉及修改Python代码本身,这不太理想,因为我不希望它只包含与测试相关的代码。相反,我发现以下bash脚本非常有用:

#!/bin/bash

rm archived_sensor_data.json
python rethinkdb_monitor_batch.py ; gedit archived_sensor_data.json

这将在
python referencedb\u monitor\u batch.py
完成后运行
gedit archived\u sensor\u data.json
命令,不管它是否成功退出。

其他答案涉及修改python代码本身,这不太理想,因为我不希望它包含仅与测试相关的代码。相反,我发现以下bash脚本非常有用:

#!/bin/bash

rm archived_sensor_data.json
python rethinkdb_monitor_batch.py ; gedit archived_sensor_data.json

这将在
python referencedb\u monitor\u batch.py
完成后运行
gedit archived\u sensor\u data.json
命令,无论它是否成功退出。

您可以始终使用第二个python文件对其进行包装,而不是修改原始python文件,这将包括
try:execfile('referencedb\u monitor\u batch.py')
之后的
except KeyboardInterrupt
子句,而不是修改原始Python文件,您可以始终使用第二个Python文件对其进行包装,该文件将在
try:execfile之后包含
except KeyboardInterrupt
子句('referencedb\u monitor\u batch.py')