怎样使用htop来监控linux系统进程[翻译]

原地址: https://www.linuxtechi.com/htop-monitor-linux-system-processes/

htop是hop命令的一个升级版实用命令。她展示了重要的系统参数,比如正在运行的任务,pids,开机时间,平均负载,内存使用和其他重要的统计负载。在展示系统参数方面,相比于她前一个hop命令htop有更高效的组织方式和变化的色彩方案。比起文本化和繁琐的top命令htop更能让用户能直观的找到他们想要的系统度量。在这篇文章中我们会展示怎样使用大多数htop命令来检索变化的系统度量。htop能在所有的linux发行版中工作,在大多数最新的版本中htop命令默认已经被预装。展示系统度量,紧紧需要输入一个简单的htop命令即可

htop

htop命令的区域

从刚刚的照片输出中,我们能清楚看到htop展示了3个区域:

头部区域:

头部区域展示了系统的cpu 内存 虚拟内存使用量 运行的任务数量 平均负载和开机时间等度量。

主体:

这个区域列出了所有正在运行的进程

尾部区域:

这部分展示了htop的菜单选项

相比于top命令htop命令包括如下高级用法

1. 能使用鼠标与htop命令交互.
2. 系统度量都用颜色标示能让你更容易的辨别.
3. htop提供了不需要引用pid就能杀死进程的方式.
4. 你能使用鼠标滑上滑下也能使用上下键来查看所有运行的进程.

在没有安装htop命令的系统中你可能会得到一些警告消息,下面是一下安装htop命令的方法:

For Debian/ Ubuntu/Mint
apt install htop
For RHEL / CentOS
yum install htop
For Fedora 22 and later
dnf install htop
htop命令使用

现在让我们换个档位,看看htop实用工具提供的各种选项

排序输出

htop提供多种选项来对你的输出内容进行排序。使用F6功能键就能查看输出选项。在出来的左边选项中,滑动选择你想要对输出排序的条件,默认情况下是使用 PERCENT_CPU 选项。

如果想使用内存使用率排序,就用下滑键选择 PERCENT_MEM 选项

以树形的格式列出进程

linux进程通常都是层级顺序,会创建子进程,如果想显示子进程,按F5功能键就可以了。

过滤进程

linux进程能依照他们各自的路径进行过滤。按下F4功能键就能过滤。这时就能在底部区域输入进程的路径。在下面的输出中。我展示了/urs/sbin这个路径的进程。

搜索进程

按下F3功能键之后你就能在终端的底部屏幕出现的区域输入进程的名字进行搜索。举例来说,在下面这个例子中,我输入了rsyslogd进程在 /usr/sbin路径中,找到之后进程就会以黄色高亮显示。

杀死一个进程

登陆htop之后滑动到你想要结束的进程上面。在这个例子中,我选择了jenkins,她的pid是1983

下一步,选择F9或者字母k之后就可以选择你想要发送的信号。在这个例子中,我选项了SIGKILL 来确保这个进程退出

最后按下 ENTER 键

怎样定制htop的输出

做一个微调就能确定你以怎样的格式输出,按下F2健。有几个选项可以改变例如颜色,字体样式,系统参数等等。

获得帮助

要获得有关如何使用htop查找路径以及如何最佳利用其中各种快捷方式的帮助,只需按F1键。各种快捷键列表和它们的作用将显示如下所示。

这就把我们带到htop命令这个主题的结尾了。欢迎评论并与你的朋友分享

nginx配置文件说明

一般配置文件说明

user www www;  #定义Nginx运行的用户和用户组
worker_processes auto;  #nginx进程数,建议设置为等于CPU总核心数.一般设置成auto
error_log /opt/nginx/log/error.log warn;  #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
pid /var/run/nginx.pid;  #进程文件
worker_rlimit_nofile 65535;   #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致.

#工作模式与连接数上限
events
{
    use epoll;      #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型.
    worker_connections 65535;      #单个进程最大连接数(最大连接数=连接数*进程数)
}

#设定http服务器
http
{
    include mime.types;   #文件扩展名与文件类型映射表
    default_type application/octet-stream;   #默认文件类型
    charset utf-8;   #默认编码
    server_names_hash_bucket_size 128;   #服务器名字的hash表大小
    client_header_buffer_size 32k;   #上传文件大小限制
    large_client_header_buffers 4 64k;   #设定请求缓
    client_max_body_size 8m; #设定请求缓

    # 开启目录列表访问,合适下载服务器,默认关闭.
    autoindex on;   # 显示目录
    autoindex_exact_size on;   # 显示文件大小 默认为on,显示出文件的确切大小,单位是bytes 改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
    autoindex_localtime on; # 显示文件时间 默认为off,显示的文件时间为GMT时间 改为on后,显示的文件时间为文件的服务器时间

    sendfile on; # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载.注意:如果图片显示不正常把这个改成off.
    tcp_nopush on;   # 防止网络阻塞
    tcp_nodelay on;   # 防止网络阻塞
    keepalive_timeout 120;   # (单位s)设置客户端连接保持活动的超时时间,在超过这个时间后服务器会关闭该链接

    # FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度.下面参数看字面意思都能理解.
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    # gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #允许压缩的页面的最小字节数,页面字节数从header偷得content-length中获取.默认是0,不管页面多大都进行压缩.建议设置成大于1k的字节数,小于1k可能会越压越大
    gzip_buffers 4 16k; #表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果
    gzip_http_version 1.1;  #压缩版本(默认1.1,目前大部分浏览器已经支持gzip解压.前端如果是squid2.5请使用1.0)
    gzip_comp_level 5;  #压缩等级.1压缩比最小,处理速度快.9压缩比最大,比较消耗cpu资源,处理速度最慢,但是因为压缩比最大,所以包最小,传输速度快
    gzip_types text/plain application/x-javascript text/css application/xml;  #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn.
    gzip_vary on;#选项可以让前端的缓存服务器缓存经过gzip压缩的页面.例如:用squid缓存经过nginx压缩的数据

    #虚拟主机的配置
    server
    {
        listen 80;  # 监听端口
        server_name abc.com;  # 域名可以有多个,用空格隔开
        rewrite ^(.*) https://$server_name$1 permanent;  # HTTP 自动跳转 HTTPS
    }

    server
    {
        listen 443 ssl;          # 监听端口 HTTPS
        server_name abc.com;

        # 日志格式设定,
        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" $http_x_forwarded_for';

        # 设置访问日志  access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; 下面指定日志的缓存大小为32k,日志写入前启用gzip进行压缩,压缩比使用默认值1,缓存数据有效时间为1分钟
        access_log /opt/nginx/log/access.log main buffer=32k gzip flush=1m;

        # 配置域名证书
        ssl_certificate     /opt/nginx/conf/cert/certificate.crt;
        ssl_certificate_key  /opt/nginx/conf/cert/private.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers  on;

        index index.html index.htm index.php;  ## 首页默认打开的文件
        root /opt/www/;  ## 项目的位置

        location ~ .*\.(php|php5)?$
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

        # 配置地址拦截转发,解决跨域验证问题
        location /mypath/{
            proxy_pass https://localhost:8080/mypath/;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        # 图片缓存时间设置
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 10d;
        }

        # JS和CSS缓存时间设置
        location ~ .*\.(js|css)?$ {
            expires 1h;
        }

        # 设定查看Nginx状态的地址.StubStatus模块能够获取Nginx自上次启动以来的工作状态,此模块非核心模块,需要在Nginx编译安装时手工指定才能使用
        location /NginxStatus {
            stub_status on;
            access_log on;
            auth_basic "NginxStatus";
            auth_basic_user_file /opt/nginx/conf/htpasswd;   #htpasswd文件的内容可以用apache提供的htpasswd工具来产生.
        }
    }
}

nginx作为负载均衡器时候的配置文件示例

events
{
    use epoll;
    worker_connections 65535;
}
http
{
    ##upstream的负载均衡,四种调度算法,调度算法1:轮询.每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响
    upstream real_server1 {
        server 192.168.0.2:8888 ;
        server 192.168.0.3:8888 ;
    }
    #调度算法2:weight(权重).可以根据机器配置定义权重.权重越高被分配到的几率越大
    upstream real_server2 {
        server 192.168.0.4:8888 weight=2;
        server 192.168.0.5:8888 weight=3;
    }
    #调度算法3:ip_hash. 每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题
    upstream real_server3 {
        ip_hash;
        server 192.168.0.6:8888 ;
        server 192.168.0.7:8888 ;
    }
    #调度算法4:url_hash(需安装第三方插件).此方法按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率.Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包
    upstream real_server4 {
        server 192.168.0.8:8888 ;
        server 192.168.0.9:8888 ;
        hash $request_uri;
    }
    #调度算法5:fair(需安装第三方插件).这是比上面两个更加智能的负载均衡算法.此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配.Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块

    #虚拟主机的配置(采用调度算法3:ip_hash)
    server
    {
        listen 80;
        server_name www.example.com;
        #对 "/" 启用反向代理
        location / {
            proxy_pass http://real_server3;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;  #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            #以下是一些反向代理的配置,可选.
            proxy_set_header Host $host;
            client_max_body_size 10m;    #允许客户端请求的最大单文件字节数
            client_body_buffer_size 128k;    #缓冲区代理缓冲用户端请求的最大字节数,
            proxy_connect_timeout 90;    #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_send_timeout 90;    #后端服务器数据回传时间(代理发送超时)
            proxy_read_timeout 90;   #连接成功后,后端服务器响应时间(代理接收超时)
            proxy_buffer_size 4k;    #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffers 4 32k;    #proxy_buffers缓冲区,网页平均在32k以下的设置
            proxy_busy_buffers_size 64k;   #高负荷下缓冲大小(proxy_buffers*2)
            proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }
    }
}

参考网址: https://mp.weixin.qq.com/s/xmn8W3x1U2LlXUbeV2ZA_A

用脚本来动态修改阿里云的安全组规则

为了安全着想,需要在阿里云的安全组上面限制只能我们公司的ip才能访问某些端口,由于公司的ip是动态变化的,每次手动修改都很繁琐,就有了下面这个脚本自动来修改规则:

  • 先安装阿里云的python sdk包
pip3 install aliyunsdkcore
pip3 install aliyun-python-sdk-core-v3
pip3 install aliyun-python-sdk-ecs
  • shell脚本内容如下,主要用来调用python脚本来执行规则
#!/bin/bash
CURRENTTIME=`date +%Y%m%d%H%M%S`
old_ip=`cat /opt/sh/oldip.txt`
real_ip=`curl  ifconfig.io`

if [ ${real_ip} != ${old_ip} ]
then
    /usr/bin/python3 /opt/sh/aliyunapi_delete.py 22/22 ${old_ip}/32 安全组ID_name  # 先删除原来的规则
    /usr/bin/python3 /opt/sh/aliyunapi_add.py 22/22 ${real_ip}/32 ssh 安全组ID_name # 再添加新的规则
    echo "${real_ip}" > /opt/sh/oldip.txt # 修改完成之后,用新的ip来替换调原来旧的ip
    echo "现在的时间是: ${CURRENTTIME}" >> /opt/sh/changeAliFirewalld.log
    echo "ip已经改变: 原ip为:${old_ip};变更后的ip为${real_ip}" >> /opt/sh/changeAliFirewalld.log
    echo "------------------------------------------------------------" >> /opt/sh/changeAliFirewalld.log
else
    echo "现在的时间是: ${CURRENTTIME}" >> /opt/sh/changeAliFirewalld.log
    echo "ip无变化,ip为:${old_ip}" >> /opt/sh/changeAliFirewalld.log
    echo "------------------------------------------------------------" >> /opt/sh/changeAliFirewalld.log
fi
  • aliyunapi_add.py 脚本的内容如下,用来添加ip规则,如果原来已经存在的相同规则的规则,阿里就不会再添加
#!/usr/bin/python3
# coding = utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupRequest
import sys
import json


class AliGroup:
    def __init__(self, access_key, access_secret, region_id):
        self.access_key = access_key
        self.access_secret = access_secret
        self.region_id = region_id

    def client(self):
        client = AcsClient(self.access_key, self.access_secret, self.region_id)
        return client

    def authorizeSecurityGroupRequest(self, port_range, source_cidr_ip, description, securitygroupid, priority =1 , ip_protocol='tcp'):
        request = AuthorizeSecurityGroupRequest.AuthorizeSecurityGroupRequest()
        request.set_SecurityGroupId(securitygroupid)
        request.set_IpProtocol(ip_protocol)
        request.set_PortRange(port_range)
        request.set_SourceCidrIp(source_cidr_ip)
        request.set_Priority(priority)
        request.set_Description(description)
        request.set_Policy('accept')
        request.set_accept_format(json)
        return request


if __name__ == '__main__':
    ali = AliGroup("your_access_key", "your_access_secret", "your_region_id")
    clt = ali.client()
    add = ali.authorizeSecurityGroupRequest(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
    res = clt.do_action_with_exception(add)
    print(res)
  • aliyunapi_delete.py 的内容如下,主要用来删除规则,只有满足条件的才会被删除
#!/usr/bin/python3
# coding = utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupRequest
import sys
import json


class AliGroup:
    def __init__(self, access_key, access_secret, region_id):
        self.access_key = access_key
        self.access_secret = access_secret
        self.region_id = region_id

    def client(self):
        client = AcsClient(self.access_key, self.access_secret, self.region_id)
        return client

    def revokeSecurityGroupRequest(self, port_range, source_cidr_ip, securitygroupid, ip_protocol='tcp'):
        request = RevokeSecurityGroupRequest.RevokeSecurityGroupRequest()
        request.set_SecurityGroupId(securitygroupid)
        request.set_IpProtocol(ip_protocol)
        request.set_PortRange(port_range)
        request.set_SourceCidrIp(source_cidr_ip)
        request.set_Policy('accept')
        request.set_accept_format(json)
        return request


if __name__ == '__main__':
    ali = AliGroup("your_access_key", "your_access_secret", "your_region_id")
    clt = ali.client()
    rem = ali.revokeSecurityGroupRequest(sys.argv[1], sys.argv[2], sys.argv[3])
    res = clt.do_action_with_exception(rem)
    print(res)
  • oldip.txt 这个里面的内容就是获取到的一个ip
you_ip_address
  • 最后再将shell脚本写入到计划任务就可以了

centos7安装postgresql11并修改默认数据库路径

安装系统为centos7.x

yum install centos-release-scl -y
cd /opt && wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
rpm -i pgdg-redhat-repo-latest.noarch.rpm
[ -f /opt/pgdg-redhat-repo-latest.noarch.rpm ] && rm /opt/pgdg-redhat-repo-latest.noarch.rpm -rf
yum clean all && yum makecache && yum install postgresql11* -y
/usr/pgsql-11/bin/postgresql-11-setup initdb 
systemctl enable postgresql-11 && systemctl stop postgresql-11
mkdir -p /opt/data/postgresql11 && chown postgres:postgres /opt/data/postgresql11 -R
cd /var/lib/pgsql/11/ && mv * /opt/data/postgresql11/  
sed -i 's/Environment=PGDATA=\/var\/lib\/pgsql\/11\/data\//Environment=PGDATA=\/opt\/data\/postgresql11\/data\//g' /usr/lib/systemd/system/postgresql-11.service
systemctl daemon-reload
systemctl start postgresql-11

在 CentOS 7 上配置 Solr

  • 安装依赖
yum install rng-tools -y  ## 该软件会自动补充熵池,避免solr运行时报错
yum install java -y  ## 安装java依赖
  • 安装solr命令
cd /opt
wget http://mirror.bit.edu.cn/apache/lucene/solr/8.2.0/solr-8.2.0.tgz
tar xzf solr-8.2.0.tgz
/opt/solr-8.2.0/bin/install_solr_service.sh /opt/solr-8.2.0.tgz
service solr start
service solr restart
chkconfig solr on
[  -f /opt/solr-8.2.0.tgz ] && rm /opt/solr-8.2.0.tgz -rf

参考文章:
https://blog.csdn.net/fukai8350/article/details/80429978
https://devops.ionos.com/tutorials/install-and-configure-apache-solr-on-centos-7/