天天看點

Linux常見服務之logrotate日志切割和supervisor程序管理

1.Logrotate:日志切割

      作用:切割日志,然後按照日期去排列,如隻保留30天内,删除30天之前的日志

[[email protected] ~]$ cd /etc/logrotate.d/
[[email protected] logrotate.d]$ ls
bootlog  httpd  mysql  nginx  php-fpm  ppp  syslog  wpa_supplicant  yum
[[email protected] logrotate.d]$  cd /var/log/nginx/

# 切割日志
[[email protected] nginx]$ sudo logrotate /etc/logrotate.d/nginx
[sudo] password for xiaokang: 
[[email protected] nginx]$ ll
total 40
-rw-r----- 1 nginx adm     0 Jun 26 12:31 access.log
-rw-r----- 1 nginx adm   678 Jun 13 08:20 access.log-20190616.gz
-rw-r----- 1 nginx adm    76 Jun 19 19:03 access.log-20190620.gz
-rw-r----- 1 nginx adm   600 Jun 25 02:09 access.log-20190625.gz
-rw-r----- 1 nginx adm  1298 Jun 25 04:00 access.log-20190626
           

2.supervisor:程序管理

        supervisor是有Python寫的,是以直接可以使用pip進行安裝使用

    1)安裝supervisor

[[email protected] ~]$ sudo pip install supervisor
           

   2) 生成supervisor配置檔案

[[email protected] ~]$ sudo mkdir /etc/supervisor
[[email protected] ~]$ sudo su -
Last login: Wed Jun 26 15:55:58 CST 2019 on pts/1
# 生成supervisor配置檔案
[[email protected] ~]# echo_supervisord_conf > /etc/supervisor/supervisor.conf
[[email protected] ~]# exit    # 退出root賬戶
logout
[[email protected] ~]$ 
           

   3)如何通過supervisor控制某個程式

[[email protected] ~]$ sudo vim /etc/supervisor/supervisor.conf
# 編輯末尾的内容如下
[include]
files = /etc/supervisor/conf.d/*.ini
           
[[email protected] ~]$ cd /etc/supervisor/
[[email protected] supervisor]$ ll
total 12
-rw-r--r-- 1 root root 9537 Jun 26 16:40 supervisor.conf
[[email protected] supervisor]$ sudo mkdir conf.d   # 建立檔案夾conf.d,用來管理程序檔案
[sudo] password for xiaokang: 
[[email protected] supervisor]$ ll
total 12
drwxr-xr-x 2 root root    6 Jun 26 16:43 conf.d
-rw-r--r-- 1 root root 9537 Jun 26 16:40 supervisor.conf
[[email protected] supervisor]$ cd conf.d/
[[email protected] conf.d]$ sudo touch redis.ini
           
# 檢視redis服務指令在哪裡
[[email protected] conf.d]$ which redis-server
/usr/local/bin/redis-server
[[email protected] conf.d]$ sudo vim redis.ini    # 建立管理Redis的檔案redis.ini
# 添加以下内容
[program:redis]
command=/usr/local/bin/redis-server   # 執行指令
autostart=true     # 随着supervisor的啟動而啟動
autorestart=true    # 自動啟動
startsecs=3   # 啟動時間
           

  啟動supervisor

# 啟動supervisor
[[email protected] conf.d]$ supervisord -c /etc/supervisor/supervisor.conf 
[[email protected] conf.d]$ ps -ef | grep sup
xiaokang  4939     1  0 16:52 ?        00:00:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisor/supervisor.conf
xiaokang  4949  4670  0 16:53 pts/2    00:00:00 grep --color=auto sup

```
特别提示!!!!
拒絕通路是因為配置檔案名不能叫做supervisor.conf必須是supervisord.conf
```
[[email protected] conf.d]$ supervisorl
-bash: supervisorl: command not found
[[email protected] conf.d]$ supervisorctl
http://localhost:9001 refused connection
supervisor> exit
[[email protected] conf.d]$ supervisorctl -c /etc/supervisor/supervisor.conf
redis                            RUNNING   pid 4940, uptime 0:01:58
supervisor> exit     # 退出supervisor互動界面
重新建立supervisor配置檔案為supervisord.conf ,并殺死supervisor.conf的程序
           
[[email protected] supervisor]$ sudo cp supervisor.conf supervisord.conf 
[[email protected] supervisor]$ ll
total 24
drwxr-xr-x 2 root root   23 Jun 26 16:51 conf.d
-rw-r--r-- 1 root root 9537 Jun 26 16:40 supervisor.conf
-rw-r--r-- 1 root root 9537 Jun 26 16:55 supervisord.conf
[[email protected] supervisor]$ ps -ef | grep sup
xiaokang  4939     1  0 16:52 ?        00:00:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisor/supervisor.conf
xiaokang  4972  4670  0 16:56 pts/2    00:00:00 grep --color=auto sup
[[email protected] supervisor]$ kill 4939
           

 重新啟動supervisor

[[email protected] supervisor]$ supervisord -c /etc/supervisor/supervisord.conf 
[[email protected] supervisor]$ ps -ef | grep supe
xiaokang  4980     1  0 16:57 ?        00:00:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
xiaokang  4986  4670  0 16:57 pts/2    00:00:00 grep --color=auto supe
[[email protected] supervisor]$ ps -ef | grep redis
xiaokang  4981  4980  0 16:57 ?        00:00:00 /usr/local/bin/redis-server *:6379
xiaokang  4988  4670  0 16:57 pts/2    00:00:00 grep --color=auto redis
[[email protected] supervisor]$ supervisorctl
redis                            RUNNING   pid 4981, uptime 0:00:53
supervisor> exit  # 退出supervisor互動界面
           

通過supervisor互動界面控制redis程序

[[email protected] supervisor]$ sudo rm -rf supervisor.conf 
[[email protected] supervisor]$ ll
total 12
drwxr-xr-x 2 root root   23 Jun 26 16:51 conf.d
-rw-r--r-- 1 root root 9537 Jun 26 16:55 supervisord.conf
[[email protected] supervisor]$ supervisorctl
redis                            RUNNING   pid 4981, uptime 0:02:04
supervisor> status
redis                            RUNNING   pid 4981, uptime 0:02:09
supervisor> stop redis    # 關閉redis程序
redis: stopped
supervisor> start redis  # 啟動redis程序
redis: started
supervisor> exit   # 退出supervisor互動界面
           

繼續閱讀