Python检测linux关机并在关机前运行命令 [英] Python detect linux shutdown and run a command before shutting down

查看:234
本文介绍了Python检测linux关机并在关机前运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测并中断linux(Ubuntu 16.04)关闭信号(例如,单击电源按钮或电池电量耗尽).我有一个始终在录制视频的python应用程序,并且我想检测到这种信号,因此在操作系统关闭之前可以正确关闭录制.

解决方案

当linux关闭时,所有进程都会收到 SIGTERM ,如果它们在一段时间超时后仍未终止,则它们将被杀死.> SIGKILL .您可以使用 signal 来实现信号处理程序,以正确关闭应用程序模块. systemd (在早期的Ubuntu版本中与 upstart 相反)在关闭时还会发送 SIGHUP .

为了验证它是否确实有效,我在两个Ubuntu VM(12.04和16.04)上尝试了以下脚本.在发出 SIGKILL 之前,系统等待10s(12.04/upstart)或90s(16.04/systemd).

该脚本将忽略 SIGHUP (否则也会不正常地终止进程),并将自从收到 SIGTERM 信号以来一直打印时间到文本文件./p>

注意:我使用 disown (内置的bash命令)将进程与终端分离.

  python signaltest.py&舍弃 

signaltest.py

 导入信号导入时间停止=错误out = open('log.txt', 'w')def停止(信号,帧):全局停止停止=真out.write('抓到SIGTERM \ n')out.flush()def ignore(sig,frsma):out.write('忽略信号%d \ n'%sig)out.flush()signal.signal(signal.SIGTERM,停止)signal.signal(signal.SIGHUP,忽略)虽然没有停止:out.write('running \ n')out.flush()time.sleep(1)stop_time = time.time()而True:out.write('%.4fs stop \ n'%(time.time()-stop_time))out.flush()time.sleep(0.1) 

打印到 log.txt 中的最后一行是:

停止后

  10.1990s 

12.04和

停止后

  90.2448s 

对于16.04.

Is it possible to detect and interrupt linux (Ubuntu 16.04) shutdown signal (e.g. power button clicked or runs out of battery). I have a python application that is always recording a video and I want to detect such signal so I close the recording properly before OS shutdown.

解决方案

When linux is shut down, all processes receive SIGTERM and if they won't terminate after some timeout they are killed with SIGKILL. You can implement a signal handler to properly shutdown your application using the signal module. systemd (opposed to upstart in earlier Ubuntu verions) additionally sends SIGHUP on shutdown.

To verfiy that this actually works, I tried the following script on two Ubuntu VMs (12.04 and 16.04). The system waits for 10s (12.04/upstart) or 90s (16.04/systemd) before issuing SIGKILL.

The script ignores SIGHUP (which would otherwise also kill the process ungracefully) and will continuously print the time since the SIGTERM signal has been received to a text file.

Note I used disown (built-in bash command) to detach the process from the terminal.

python signaltest.py &
disown

signaltest.py

import signal
import time

stopped = False

out = open('log.txt', 'w')

def stop(sig, frame):
    global stopped
    stopped = True
    out.write('caught SIGTERM\n')
    out.flush()

def ignore(sig, frsma):
    out.write('ignoring signal %d\n' % sig)
    out.flush()

signal.signal(signal.SIGTERM, stop)
signal.signal(signal.SIGHUP, ignore)

while not stopped:
    out.write('running\n')
    out.flush()
    time.sleep(1)

stop_time = time.time()
while True:
    out.write('%.4fs after stop\n' % (time.time() - stop_time))
    out.flush()
    time.sleep(0.1)

The last line printed into log.txt was:

10.1990s after stop

for 12.04 and

90.2448s after stop

for 16.04.

这篇关于Python检测linux关机并在关机前运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆