linux_cmds
一、systemd和supervisor
01 systemd
systemd是linux的系统工具,是linux的init实现,用来启动守护进程,已经成为大多数发行版的标准配置。
可以通过systemctl --version命令来查看使用的版本。
常用的systemd的命令
#启动/停止/重启/kill/reload 一个服务
$ systemctl start/stop/restart/kill/reload xxx.service
#重载所有修改过的配置文件
$ systemctl daemon-reload
#显示某个Unit的所有底层参数
$ systemctl show xxx.service
#查看某个unit的状态
$ systemctl status [xxx.service]
#查看systemd管理的所有服务
$ systemctl list-units --type=service
#查看所有服务,包括管理的服务以及可用但未启动的服务
$ systemctl list-unit-files --type=service
# 查看某个unit的日志
$ journalctl -u xxx.service
systemd默认从目录/etc/systemd/system/读取配置文件。但是这里大部分都是符号链接,指向目录/usr/lib/systemd/system/,真正的文件存放在这个目录。
而systemctl enable xxx.service等同于ln -s '/usr/lib/systemd/system/xxx.service' '/etc/systemd/system/multi-user.target.wants/xxx.service'
可以在unit的配置文件的[Service]部分,指定User=和Group=指定运行服务的用户和用户组。
02 supervisor
一个用python编写的进程管理程序,除了管理进程,还可以用来做开机启动。
安装supervisor
pip install supervisor 或 apt install supervisor
supervisor自身配置
从模板生成echo_supervisord_conf > /etc/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ; socket 文件的 mode,默认是 0700
;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid
;[inet_http_server] ; HTTP 服务器,提供 web 管理界面
;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性
;username=user ; 登录管理后台的用户名
;password=123 ; 登录管理后台的密码
[supervisord]
logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB
logfile_backups=10 ; 日志文件保留备份数量默认 10
loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200 ; 可以打开的进程数的最小值,默认 200
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord
; 包含其他的配置文件 一般用于存放下文中守护进程的配置
[include]
files = /etc/supervisord.d/*.ini
被守护进程配置
假设有个java应用包web.jar,位于/home/user/projects/
在/etc/supervisord.d/下新建web.ini文件,写入
[program:web]
directory = /home/user/projects ; 程序的启动目录
command = java -jar web.jar ; 启动命令可以根据实际情况加入相应的参数
;进程名称
process_name = %(program_name)s_%(process_num)02d
;启动设置
numprocs = 1 ;进程数
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数 ,默认是 3
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile = /data/logs/usercenter_stdout.log
;停止信号,默认TERM
;中断:INT (类似于Ctrl+C)(kill -INT pid),退出后会将写文件或日志(推荐)
;终止:TERM (kill -TERM pid)
;挂起:HUP (kill -HUP pid),注意与Ctrl+Z/kill -stop pid不同
;从容停止:QUIT (kill -QUIT pid)
stopsignal=INT
操作
# 启动
$ supervisord -c /etc/supervisord.conf
# 关闭
$ supervisorctl -c /etc/supervisord.conf shutdown
# 重启
$ supervisorctl -c /etc/supervisord.conf reload
# 进入交互终端
$ supervisorctl
# 查看进程状态
$ supervisorctl status
# 启动/停止/重启指定的守护程序
$ supervisorctl start/stop/restart <progname>
# 读取有更新(增加)的配置文件,不会启动新添加的程序
$ supervisorctl reread
# 重启配置文件修改过的程序
$ supervisorctl update
03 使用systemd启动supervisor
在机器启动之后,自动启动supervisor,需要把supervisor配置到systemd中。
-
进入
/usr/lib/systemd/system/目录下,增加文件supervisord.service,文件内容如下:[Unit]
Description=Process Monitoring and Control Daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target -
激活
systemctl enable supervisord.service -
启动
systemctl start supervisord.service -
关闭
systemctl stop supervisord.service -
如果修改了配置,重新加载
systemctl reload supervisord.service
摘录文档:
supervisord进程守护的使用以及与systemd的对比 - 掘金
二、树状结构显示进程
# ps axjf
# ps -ejH