linux主要使用supervise来管理常驻进程。基于supervise的两个比较重要的工具是DaemontoolsSupervisor

实际上,supervise也算Daemontools的一个工具。Daemontools是svscanboot,svscan,supervise,svc,svok,svstat等一系列工具的合集。

安装

执行安装后,所有的命令都放到了/command目录,并软链到/usr/local/bin/下面。

并且新建了/service目录来放置常驻脚本。

关系

安装完后,可以看到两个进程启动了。

root     19907  0.0  0.0   1936   508 ?        Ss   17:12   0:00 /bin/sh /command/svscanboot
root 19909 0.0 0.0 1880 376 ? S 17:12 0:00 svscan /service

svscanboot启动svscan监视/service目录,svscan则为/service的每个进程都启动一个supervise服务。

supervise s 执行./s/run,如果s/down文件存在,则需要使用svc手动启用。(机器重启的时候防止自动启用)

如果往/service下面加入服务脚本,则可以在后台看到下面的进程。

root@test2:/opt/tiger/graphite_client/tsar-client_run# ps aux|grep supervise
root 3945 0.0 0.0 3932 40 ? S 2013 0:00 supervise location_search_8920
root 3946 0.0 0.0 3932 28 ? S 2013 0:00 supervise fenci_run
root 3952 0.0 0.0 3932 76 ? S 2013 44:04 supervise tsar-client_run
root 3953 0.0 0.0 3932 52 ? S 2013 0:00 supervise sentinel_run
root 3954 0.0 0.0 3932 20 ? S 2013 0:00 supervise qiuzu_solr

supervise的状态信息以2进制的形式存放在s/supervise下面,并且提供了下面的工具来操作:

  • svstat: 读取状态信息
  • svc: 启动/停止/挂起等
  • svok: 检查是否运行成功
  • svscan:可靠的启动/service目录下的服务。如果某个服务加入后,没有启动,可以调用此命令,强制启动。

加入一个新服务

最简单的方式是建立一个文件夹

testsvc
├── main.py
└── run

写入文件内容:

yijingping@yjp-pc:~/testsvc$ cat main.py
#!/usr/bin/python
import time
import logging while True:
time.sleep(1)
logging.info('sleep 1 second')
logging.error('sleep 1 second') yijingping@yjp-pc:~/testsvc$ cat run
#!/bin/sh
exec ./main.py 1>/var/log/main.py.log 2>&1

然后在/service目录下建立软链接

$ sudo ln -s /path/to/testsvc

这个时候可以检查一下服务是否正在运行:

yijingping@yjp-pc:~$ sudo svstat /service/testsvc
/service/testsvc: up (pid 4204) 962 seconds
yijingping@yjp-pc:~$ ps aux|grep supervise
root 4203 0.0 0.0 1716 248 ? S 09:37 0:00 supervise testsvc
1000 5631 0.0 0.0 3784 792 pts/3 S+ 09:54 0:00 grep supervise
yijingping@yjp-pc:~$ tree /service/testsvc
/service/testsvc
├── main.py
├── run
└── supervise [error opening dir] 1 directory, 2 files

上面这种方式的坏处是必须以root用户运行,如果想以其他用户运行,则需要做如下改进,假设用户为tiger,id为1001:

tigersvc
├── main.py
├── real_run
└── run

文件内容:

tiger@yjp-pc:~/tigersvc$ cat run
#!/bin/sh
who=$(id -u)
if [ $who -eq 0 ]; then
exec /usr/local/bin/setuidgid tiger ./real_run
elif [ $who -eq 1001 ];then
exec ./real_run
else
echo "neither root nor tiger"
fi
tiger@yjp-pc:~/tigersvc$ cat real_run
#!/bin/sh
exec ./main.py 1>/var/log/tiger/main2.py.log 2>&1

加入服务后,查看后台进程以tiger为用户在运行:

yijingping@yjp-pc:/service$ ps aux|grep main.py
tiger 24682 0.0 0.1 7052 3924 ? S 13:47 0:00 /usr/bin/python ./main.py

管理服务

使用svstat来查看服务

yijingping@yjp-pc:/service$ sudo svstat testsvc
testsvc: down 20 seconds, normally up
yijingping@yjp-pc:/service$ sudo svstat tigersvc
tigersvc: up (pid 25046) 230 seconds

使用svc来管理服务

command mnemonic    signal  action
svc -u up bring service up
svc -d down put service down (stays down)
svc -o once run service once (don't restart)
svc -k kill SIGKILL send service KILL signal

如果要重启,必须先svc -d s,再svc -u s

其他工具

log工具:

  • The readproctitle program
  • The multilog program
  • The tai64n program
  • The tai64nlocal program

环境工具:

  • The setuidgid program
  • The envuidgid program
  • The envdir program
  • The softlimit program
  • The setlock program

阅读原文

Daemontools和Supervisor管理linux常驻进程的更多相关文章

  1. 使用Supervisor管理Linux进程

    使用Supervisor管理Linux进程 简介 Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由python编写,提供了大量的功能来实现对进程的管理. 安装 sudo ...

  2. Supervisor 管理后台守护进程

    Supervisor 管理后台守护进程 参考原文如下: http://codinn.com/people/brant/notes/110948/ 做了一些注释 +++++++++++引用开始+++++ ...

  3. Supervisor(Linux/Unix进程管理工具)安装与配置

    参考链接:https://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyt ...

  4. [进程管理]linux 下 进程和线程的区别(baidu 面试)

    进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流,是CPU调度和分派的 ...

  5. 【云计算】使用supervisor管理Docker多进程-ntpd+uwsgi+nginx示例最佳实践

    supervisor安装启动: apt-get install supervisor -y # start supervisord nodaemon /usr/bin/supervisord --no ...

  6. linux 使用进程管理工具 supervisor

    1.supervisor是使用python进行开发的运行在linux服务器上的进程管理工具 老版本的supervisor需要运行在python2环境,如果需要使用supervisor管理python3 ...

  7. Supervisor安装与配置(Linux/Unix进程管理工具)

    原文链接:http://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyth ...

  8. Linux服务:使用Supervisor管理进程

    一.简介 由于基本每个公司都会用到supervisor这个进程管理工具,这里简单阐述一下. Supervisor (http://supervisord.org) 是一个用Python写Linux下的 ...

  9. .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控

    一.crontab使用详解 概述:任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类: (1)系统工作:有些重要的工作必须周而 复始地执行. (2)个别用户工作:个别用户可能希望执 行 ...

随机推荐

  1. ViewPager实现无限循环

    引言 这两天在项目里实用到ViewPager来做广告运营位展示.看到如今非常多APP的广告运营位都是无限循环的,所以就研究了一下这个功能的实现. 先看看效果 从一个方向上一直滑动.么有滑到尽头的感觉. ...

  2. MoveIt运动规划-1

    按照帮助文档Setup Assistant Tutorial中的描述,用moveit_setup_assistant图形界面一步步配置机器人,最后生成机器人的配置文件保存到指定的文件夹中. 以UR5机 ...

  3. python之模块配置文件ConfigParser(在python3中变化较大)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:py ...

  4. β particle, α particle, γ ray, ionization chamber

    Alpha particles consist of two protons and two neutrons bound together into a particle identical to ...

  5. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  6. MBProgressHUD 的类扩展方法用法

    #import "MBProgressHUD.h" @interface MBProgressHUD (Add) + (void)showError:(NSString *)err ...

  7. 【RS】BPR:Bayesian Personalized Ranking from Implicit Feedback - BPR:利用隐反馈的贝叶斯个性化排序

    [论文标题]BPR:Bayesian Personalized Ranking from Implicit Feedback (2012,Published by ACM Press) [论文作者]S ...

  8. MyStack

    #pragma once class MyQueue { public: MyQueue(); ~MyQueue(); void Insert(int aValue); int Top(); void ...

  9. IOC 之深入理解 Spring IoC

    在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...

  10. 基于js利用经纬度进行两地的距离计算(转)

    转自:http://www.storyday.com/html/y2009/2212_according-to-latitude-and-longitude-distance-calculation- ...