推荐学习书目
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 · Jul 30, 2014 · 4302 views
    This topic created in 4332 days ago, the information mentioned may be changed or developed.
    6 replies    2014-07-30 23:30:27 +08:00
    izoabr
        1
    izoabr  
       Jul 30, 2014
    linux下,以前是init6和init0来弄的,win下不知道,是不是注册表里有相关的条目可以动啊。
    wy315700
        2
    wy315700  
       Jul 30, 2014   ❤️ 1
    关机的时候会给进程发送SIGTERM消息的吧

    监听一下就行了
    sandtears
        3
    sandtears  
       Jul 30, 2014
    Windows 的组策略管理器(gpedit.msc) 里面有关机脚本的设定。
    见下图:



    Linux 的话可以写在 /etc/rc0.d/ 里面
    decken
        4
    decken  
    OP
       Jul 30, 2014
    @sandtears 用户程序的话没可能让用户这样做吧。我的想法是直接在程序中处理
    sandtears
        5
    sandtears  
       Jul 30, 2014   ❤️ 1
    @decken Linux 的话安装的时候把脚本丢进去就是了。 Windows 的话你看看有没有改注册表的地方
    tmqhliu
        6
    tmqhliu  
       Jul 30, 2014   ❤️ 1
    直接用 Win32 API 从消息循环里监听 WM_QUERYENDSESSION 或 WM_ENDSESSION
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   937 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 21:11 · PVG 05:11 · LAX 14:11 · JFK 17:11
    ♥ Do have faith in what you're doing.