V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
decken
V2EX  ›  Python

想做一些关机清尾工作,在 win 和 Linux 平台运行的 Python 脚本如何监听系统关机重启的操作?

  •  
  •   decken ·
    DecKen · 2014-07-30 20:38:22 +08:00 · 3781 次点击
    这是一个创建于 3575 天前的主题,其中的信息可能已经有所发展或是发生改变。
    6 条回复    2014-07-30 23:30:27 +08:00
    izoabr
        1
    izoabr  
       2014-07-30 20:41:41 +08:00
    linux下,以前是init6和init0来弄的,win下不知道,是不是注册表里有相关的条目可以动啊。
    wy315700
        2
    wy315700  
       2014-07-30 20:42:37 +08:00   ❤️ 1
    关机的时候会给进程发送SIGTERM消息的吧

    监听一下就行了
    sandtears
        3
    sandtears  
       2014-07-30 22:10:01 +08:00
    Windows 的组策略管理器(gpedit.msc) 里面有关机脚本的设定。
    见下图:



    Linux 的话可以写在 /etc/rc0.d/ 里面
    decken
        4
    decken  
    OP
       2014-07-30 22:29:44 +08:00
    @sandtears 用户程序的话没可能让用户这样做吧。我的想法是直接在程序中处理
    sandtears
        5
    sandtears  
       2014-07-30 23:03:23 +08:00   ❤️ 1
    @decken Linux 的话安装的时候把脚本丢进去就是了。 Windows 的话你看看有没有改注册表的地方
    tmqhliu
        6
    tmqhliu  
       2014-07-30 23:30:27 +08:00   ❤️ 1
    直接用 Win32 API 从消息循环里监听 WM_QUERYENDSESSION 或 WM_ENDSESSION
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1460 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:15 · PVG 01:15 · LAX 10:15 · JFK 13:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.