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
Ewig
V2EX  ›  Python

Python 如何看某个脚本的状态

  •  
  •   Ewig · 2018-12-20 17:47:52 +08:00 · 1800 次点击
    这是一个创建于 1947 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/python3
    import sys
    import datetime
    import os
    import time
    if __name__ == '__main__':
    while 1:
    startTime=datetime.datetime.now()
    main_path='./main.py'
    py=sys.executable
    status=os.system(r'{} {} {} {}'.format(py, main_path, sys.argv[1],sys.argv[2]))
    if status==0:
    time.sleep(120)
    else:
    endTime=datetime.datetime.now()
    if endTime-startTime>60*60:
    break



    我现在在程序里启一个脚本,我想判断这个脚本运行了超过 1 个小时就 kill 掉,如何写?
    5 条回复    2018-12-21 10:50:25 +08:00
    Cooky
        1
    Cooky  
       2018-12-20 18:34:22 +08:00 via Android
    multiprocess
    lakechan96
        2
    lakechan96  
       2018-12-20 22:42:57 +08:00
    然后开一个线程开始 sleep,超时就 kill
    Ewig
        3
    Ewig  
    OP
       2018-12-21 09:18:20 +08:00
    @lakechan96 具体如何操作
    Ewig
        4
    Ewig  
    OP
       2018-12-21 09:20:09 +08:00
    @Cooky 具体如何操作
    Cooky
        5
    Cooky  
       2018-12-21 10:50:25 +08:00 via Android
    @Ewig RTFM
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2755 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:49 · PVG 20:49 · LAX 05:49 · JFK 08:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.