用 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

  • 程序员大咖整理发布,转载请联系作者获得授权

【点击成为Java大神】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值