當前位置:
首頁 > 知識 > 用 Python 管理系統進程

用 Python 管理系統進程

點擊上方

Python開發

」,選擇「置頂公眾號」


關鍵時刻,第一時間送達!



Supervisor 可以啟動、停止、重啟 * nix 系統中的程序。也可以重啟崩潰的程序。


supervisord 的一個守護進程,用於將指定的進程當做子進程來運行。

supervisorctl 是一個客戶端程序,可以查看日誌並通過統一的會話來控制進程。


看例子:


我們寫了一個 py 腳本,用於往 log 文件中記錄一條當前的時間。

  1. root@ubuntu

    :/

    home

    /

    zoer  

    # cat daemon.py

  2. #!/usr/bin/env python

  3. import

    time

  4. import

    os

  5. time

    .

    sleep

    (

    1

    )

  6. f

    =

    open

    (

    "log"

    ,

    "a"

    )

  7. t

    =

    time

    .

    time

    ()

  8. f

    .

    write

    (

    str

    (

    t

    ))

  9. f

    .

    write

    (

    "
    "

    )

  10. f

    .

    close

    ()


安裝過程就不說了。


安裝完畢supervisor之後【將配置文件放在/etc下】。修改配置文件,在最後增加如下內容:

  1. [

    program

    :

    ddd

    ]

  2. command

    =

    /home/

    zoer

    /

    daemon

    .

    py  

  3. autorestart

    =

    true


然後我們啟動 supervisor 並啟動 daemon.py 的執行。

  1. root@ubuntu

    :/

    home

    /

    zoer  

    # supervisord

  2. /

    usr

    /

    local

    /

    lib

    /

    python2

    .

    7

    /

    dist

    -

    packages

    /

    supervisor

    -

    3.0b1

    -

    py2

    .

    7.egg

    /

    supervisor

    /

    options

    .

    py

    :

    286

    :

    UserWarning

    :

    Supervisord

    is

    running

    as

    root

    and

    it

    is

    searching

    for

    its configuration file

    in

    default locations

    (

    including its current working directory

    )

    you probably want to specify a

    "-c"

    argument specifying an absolute path to a configuration file

    for

    improved security

    .

  3.    

    "Supervisord is running as root and it is searching "

  4. root@ubuntu

    :/

    home

    /

    zoer  

    # supervisorctl

  5. ddd                              STARTING

  6. supervisor

    >

    start ddd

  7. ddd

    :

    ERROR

    (

    already started

    )

  8. supervisor

    >

    stop ddd

  9. ddd

    :

    stopped

  10. supervisor

    >

    start ddd

  11. ddd

    :

    started

  12. supervisor

    >


從上面的例子中,看到,可以通過 start 或者 stop 命令來啟動或者停止 ddd 這個進程。 ddd 這裡就是我們在配置文件中增加的內容( daemon.py 這個腳本)。


也可以使用 restart 。如下:

  1. supervisor

    >

    restart ddd

  2. ddd

    :

    stopped

  3. ddd

    :

    started


下面我們測試一下,假設說我們手動 kill 掉了 ddd 這個進程,那麼 ddd 會自動恢復執行嗎?


為了做實驗,把代碼修改如下:

  1. root@ubuntu

    :/

    home

    /

    zoer  

    # cat daemon.py

  2. #!/usr/bin/env python

  3. import

    time

  4. import

    os

  5. while

    True

    :

  6.    time

    .

    sleep

    (

    1

    )

  7.    f

    =

    open

    (

    "log"

    ,

    "a"

    )

  8.    t

    =

    time

    .

    time

    ()

  9.    f

    .

    write

    (

    str

    (

    t

    ))

  10.    f

    .

    write

    (

    "
    "

    )

  11.    f

    .

    close

    ()


通過 ps 可以找到這個進程的 id :

  1. root      

    9354

     

    0.2

     

    0.4

     

    10924

     

    4200

    ?

           S    

    23

    :

    16

     

    0

    :

    00

    python

    /

    home

    /

    zoer

    /

    daemon

    .

    py

  2. root      

    9395

     

    0.0

     

    0.0

     

    4392

     

    832

    pts

    /

    3

       S

    +

     

    23

    :

    17

     

    0

    :

    00

    grep

    --

    color

    =

    auto daemon

  3. root@ubuntu

    :/

    home

    /

    zoer


看下面的操作:

  1. root@ubuntu

    :/

    home

    /

    zoer  

    # rm log;touch log;kill 9354

  2. root@ubuntu

    :/

    home

    /

    zoer  

    # cat log

  3. 1364710712.51

  4. root@ubuntu

    :/

    home

    /

    zoer  

    # cat log

  5. 1364710712.51

  6. 1364710713.51

  7. root@ubuntu

    :/

    home

    /

    zoer  

    # cat log

  8. 1364710712.51

  9. 1364710713.51

  10. root@ubuntu

    :/

    home

    /

    zoer  

    # cat log

  11. 1364710712.51

  12. 1364710713.51

  13. 1364710714.52

  14. root@ubuntu

    :/

    home

    /

    zoer  

    # cat log

  15. 1364710712.51

  16. 1364710713.51

  17. 1364710714.52

  18. 1364710715.52


刪除了 log 文件,並且重新創建。然後幹掉了 daemon.py 的那個進程。會發現 log 內容又重新有新的內容了。再次 ps 查看進程號。

  1. root      

    9429

     

    0.1

     

    0.4

     

    10924

     

    4200

    ?

           S    

    23

    :

    18

     

    0

    :

    00

    python

    /

    home

    /

    zoer

    /

    daemon

    .

    py

  2. root      

    9440

     

    0.0

     

    0.0

     

    4392

     

    828

    pts

    /

    3

       S

    +

     

    23

    :

    19

     

    0

    :

    00

    grep

    --

    color

    =

    auto daemon

  3. root@ubuntu

    :/

    home

    /

    zoer


會發現進程號已經變成9429了。說明 supervisor 已經重啟了被幹掉了的進程。






  • 作者:imzoer@blog.csdn.net



  • Python開發整理髮布,轉載請聯繫作者獲得授權


【點擊成為Java大神】

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 Python開發 的精彩文章:

Python 標準庫之 collections 使用教程
如何開發一個基於 Docker 的 Python 應用

TAG:Python開發 |