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. Eclipe快捷键

    常用的快捷键其实不多,但是用的少了又忘记了,所以讲常用的快捷键整理一下,方便以后自己来看. Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Q 定位到最后 ...

  2. SpringBoot集成MyBatis的分页插件PageHelper

    俗话说:好

  3. 微信小程序开发学习资料

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  4. Mac终端查看sqlite3数据库、表数据等(含sqlite可视化工具下载)

    背景: 有时候,我们在用FMDB等库处理iOS数据库时,沙盒里保存的数据库格式为.sqlite3. 那么,我们如何查看这个数据库呢? 其实有两种方法: 1.借助工具 - Sqlite Database ...

  5. windbg(1)

    1.http://www.cnblogs.com/huangyong9527/category/384128.html 2.http://www.cnblogs.com/pugang/category ...

  6. Rplidar学习(一)—— 开发套件初识

    一.简介 RPLIDAR A1 开发套装包含了方便用户对 RPLIDAR A1 进行性能评估和早期开发所需的配套工具. 用户只需要将 RPLIDAR A1 模组与 PC 机连接,即可在配套的评估软件中 ...

  7. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. Rust hello world !

    特点: 安全,速度,并发 文件:hello_world.rs 代码: fn main() { println!("hello world!"); } 执行:rustc hello_ ...

  9. 微信支付HTTPS服务器证书验证指引

    1. 背景介绍 2. 常见问题 3. 验证证书 4. 安装证书 背景介绍 微信支付使用HTTPS来保证通信安全, 在HTTPS服务器上部署了由权威机构签发的证书, 用于证明微信支付平台的真实身份. 商 ...

  10. ActiveX IE保护模式下的低权限操作路径及Windows操作系统特殊路径

    参考理解IE保护模式:https://blog.csdn.net/xt_xiaotian/article/details/5336809 文件帮助类: public class FileHelp { ...