苏苏的博客

简约至极

Supervisord

Supervisord可以通过sudo easy_install supervisor安装,当然也可以通过Supervisord官网下载后解压并转到源码所在的文件夹下执行setup.py install来安装。

使用easy_install必须安装setuptools打开https://pypi.org/project/setuptools/#files

或者

  1. 安装easy_install

sudo yum install python-setuptools-devel 2. 安装Supervisor

easy_install supervisor    3. 生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

安装完有两个可执行文件 supervisorctl supervisord

配置文件

/etc/supervisord.conf

启动

/usr/bin/supervisord -c /etc/supervisord.conf

配置文件示例

[unix_http_server]
file=/dev/shm/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
user=root		     ;

; 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:///dev/shm/supervisor.sock ; use a unix:// URL  for a unix socket

[program:php-fpm]
command = /usr/local/sbin/php-fpm --nodaemonize --fpm-config /usr/local/etc/php-fpm.d/www.conf
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=/usr/sbin/nginx -g "daemon off; error_log /dev/stderr info;"
autostart=true
autorestart=true
priority=10
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:crond]
command=/usr/sbin/crond -f
numprocs=1
autostart=true
autorestart=true
priority=5
startsecs=5
startretries=3
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0


[include]
files = /etc/supervisor/conf.d/*.conf

参考 http://www.cnblogs.com/jasonkoo/articles/3750638.html

supervisorctl 控制命令

supervisorctl start programxxx

supervisorctl stop programxxx

supervisorctl restart programxxx

supervisorctl stop all 停止全部进程,注:start、restart、stop都不会载入最新的配置文件。

supervisorctl reload 载入最新的配置文件,并按新的配置启动、管理所有进程。

supervisord 目前不支持 Python 3,必须是需要python2

类似工具 https://mmonit.com/monit/

一个简单的配置/etc/monitrc

set daemon  30
set log /var/log/monit.log
set httpd unixsocket /var/run/monit.sock allow user:pass
set daemon  30
set log /var/log/monit.log
set httpd unixsocket /var/run/monit.sock allow user:pass

check process crond with matching crond
start program = "/usr/local/bin/crond" with timeout 10 seconds
stop program = "/usr/local/bin/busybox killall crond" with timeout 10 seconds

可以使用 monit procmatch pattern 来查看规则匹配到的所有进程

process matching 匹配到多个,monit 会选择 uptime 最大的那个处理。

配置文件的权限需要是 700 否则报错The control file '/etc/monitrc' permission 0644 is wrong, maximum 0700 allowed

set daemon  30
set log /var/log/monit.log
set httpd unixsocket /var/run/monit.sock allow user:pass

check process crond with matching crond
start program = "/usr/local/bin/crond" with timeout 10 seconds
stop program = "/usr/local/bin/busybox killall crond" with timeout 10 seconds

check process redis-server with pidfile /var/run/redis-server.pid
start program = "/usr/local/bin/redis-server /etc/redis.conf"
stop  program = "/usr/local/bin/busybox killall redis-server"
if failed port 6379 then restart

监控 mysql-server

check process mysql with pidfile /run/mysqld/mysqld.pid
    start program = "/usr/sbin/service mysql start" with timeout 60 seconds
    stop program  = "/usr/sbin/service mysql stop"
    if failed unixsocket /var/run/mysqld/mysqld.sock then restart

发现进程挂了以后发送通知

set daemon 30
set log /var/log/monit.log
set httpd unixsocket /var/run/monit.sock allow user:pass

check process webpack matching webpack
if does not exist then exec "/usr/bin/curl -sL https://xx.send?text=!~~"

对于非后台进程需要&

set daemon 5
set log /var/log/monit.log
set httpd unixsocket /var/run/monit.sock allow user:pass


check process webpack matching sleep
start program = "/bin/sh -c 'sleep 40 &'" with timeout 3 seconds
stop program = "/bin/sh -c 'pkill -f sleep'" with timeout 10 seconds

https://github.com/logrotate/logrotate

alpine 中 apk add logrotate

配置文件

/etc/logrotate.conf

示例配置文件

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# exclude alpine files
tabooext + .apk-new

# uncomment this if you want your log files compressed
compress

# main log file
/var/log/messages {}

# apk packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may be also be configured here.

/etc/logrotate.d 下可以存放各个程序的配置

/etc/logrotate.d/nginx

/var/www/html/logs/*.log {
    create 0644 nginx nginx
    daily
    rotate 999
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /var/run/nginx.pid  2>/dev/null` 2>/dev/null || true
    endscript
}
/var/log/influxdb/influxd.log {
    daily
    rotate 7
    missingok
    dateext
    copytruncate
    compress
}

logrotate 只是做日期切割,具体的执行还依赖crontab

一般安装好logrotate后,就会创建crontab的配置 , 创建的文件位于 /etc/periodic/daily/

/etc/periodic/daily/logrotate

#!/bin/sh

if [ -f /etc/conf.d/logrotate ]; then
	. /etc/conf.d/logrotate
fi

if [ -x /usr/bin/cpulimit ] && [ -n "$CPULIMIT" ]; then
	_cpulimit="/usr/bin/cpulimit --limit=$CPULIMIT"
fi

$_cpulimit /usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

同时 corntab 里包含

# do daily/weekly/monthly maintenance
# min	hour	day	month	weekday	command
*/15	*	*	*	*	run-parts /etc/periodic/15min
0	*	*	*	*	run-parts /etc/periodic/hourly
0	2	*	*	*	run-parts /etc/periodic/daily
0	3	*	*	6	run-parts /etc/periodic/weekly
0	5	1	*	*	run-parts /etc/periodic/m

crontab 的规则位于 /var/spool/cron/crontabs , 以用户名为文件

crond 也需要常驻内存,logrotate 才能工作。

crond 默认的配置目录就是 /var/spool/cron/crontabs , 没有此目录,则无法运行

alpine 中直接 crond 就可以后台运行, crond -f 则前台运行

syslogd 后台运行,并将日志存储于 /var/log/messages

BusyBox v1.24.2 (2017-01-18 14:13:46 GMT) multi-call binary.

Usage: syslogd [OPTIONS]

System logging utility

	-n		Run in foreground
	-R HOST[:PORT]	Log to HOST:PORT (default PORT:514)
	-L		Log locally and via network (default is network only if -R)
	-C[size_kb]	Log to shared mem buffer (use logread to read it)
	-K		Log to kernel printk buffer (use dmesg to read it)
	-O FILE		Log to FILE (default:/var/log/messages, stdout if -)
	-s SIZE		Max size (KB) before rotation (default:200KB, 0=off)
	-b N		N rotated logs to keep (default:1, max=99, 0=purge)
	-l N		Log only messages more urgent than prio N (1-8)
	-S		Smaller output
	-D		Drop duplicates
	-f FILE		Use FILE as config (default:/etc/syslog.conf)

BusyBox v1.27.1 (2017-07-18 19:24:44 CEST) multi-call binary.

Usage: crond -fbS -l N -d N -L LOGFILE -c DIR

	-f	Foreground
	-b	Background (default)
	-S	Log to syslog (default)
	-l N	Set log level. Most verbose 0, default 8
	-d N	Set log level, log to stderr
	-L FILE	Log to FILE
	-c DIR	Cron dir. Default:/var/spool/cron/crontabs

新版busybox 1.27 中 crond 执行的任务命令ps aux 已经查看不到了, 但是通过日志可以看到实际是运行了.

syslogd -C1024 启动的,使用 logread 获取日志

在 alpine中 在crond 默认的日志是输出到syslogd的

BusyBox v1.27.2 (2017-11-01 22:58:00 UTC) multi-call binary.

Usage: crond -fbS -l N -d N -L LOGFILE -c DIR

	-f	Foreground
	-b	Background (default)
	-S	Log to syslog (default)
	-l N	Set log level. Most verbose 0, default 8
	-d N	Set log level, log to stderr
	-L FILE	Log to FILE
	-c DIR	Cron dir. Default:/var/spool/cron/crontabs

启动crond,必须创建文件夹/var/spool/cron/crontabs 或者指定一个已存在文件夹。

crond 启动以后,配置文件直接写在 /var/spool/cron/crontabs

文件修改后无需干预,自动生效。

此文件夹下的同名文件就是执行crond里命令的用户,例如 root 的 cron配置 www-data 的cron配置等。

syslogd 启动以后默认是使用IPC通信的. 建立 /dev/log socket 文件.

可执行程序 logger 便可向syslogd 发送日志消息。

如果没有/dev/log 或者 syslogd 未运行,logger将静默忽略