• Linux记录-一些常用操作


    1.查杀三天前的进程

    ps -ef|grep 进程名 |grep `date +"%b%d" --date '2 days ago'`|awk '{print $2}'|xargs kill -9

    2.配置jdk

    export JAVA_HOME=/usr/jdk/jdk1.8.0_192
    export JRE_HOME=/usr/jdk/jdk1.8.0_192/jre
    export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
    export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
    export JAVA_HOME JRE_HOME CLASS_PATH PATH

    3.新建用户

    groupadd -g 6000 apps
    useradd -s /bin/bash -G apps -m app
    echo "Apps@123" | passwd ‐‐stdin "app"

    4.selinux
    sed -i '/^SELINUX/s/=.*/=disabled/' /etc/selinux/config
    setenforce 0
    5.sudo
    vim /etc/sudoers.d/app
    app ALL=(ALL) ALL
    app ALL=(ALL) NOPASSWD: ALL
    Defaults !env_reset
    6.免密
    su app
    ssh-keygen -t rsa
    cat ~/.ssh/id_rsa.pub >> /home/app/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys

    合并id_rsa.pub到authorized_keys

    7.划分磁盘
    查看分区(fdisk -l,没有Start-End磁盘分区表示可用)
    添加分区(fdisk /dev/vdb n p 回车 回车 w)
    查看新建的分区(fdisk -l)
    格式化分区:mkfs.ext4 /dev/vdb1
    新建目录mkdir /data,并挂载分区(mount /dev/vdb1 /data)
    永久写入磁盘:将分区信息写入/etc/fstab: echo '/dev/vdb1 /data ext4 defaults 0 0' >> /etc/fstab

    8.修改swap大小
    cd /data
    dd if=/dev/zero of=/data/swapfile128G bs=1024 count=134217728
    mkswap /data/swapfile128G
    swapon /data/swapfile128G
    cat /proc/swaps
    echo '/data/swapfile128G swap swap defaults 0 0' >> /etc/fstab

    9.关闭swap
    swapoff -a
    echo 0 >/proc/sys/vm/swappiness
    echo 'vm.swappiness=0' >> /etc/sysctl.conf

    10.检测服务是否存在

     

    #!/bash/sh
    
    #Check if the fate service exists
    
    
    set -e
    
    service_name=$1
    port=$2
    
    check_port() {
            /usr/sbin/lsof -i:$port | grep -i "listen"
    }
    
    #check is not exist
    check_isnotExist(){
        if check_port $port
        then
            value=0
            echo $service_name "is running" 
        else
           value=1
           echo $service_name "is dead"
        fi
        echo "{"$service_name","$value"}"
    }
    
    if [[ $2 == "" ]]
    then
     echo "usage: $0 {mysql|redis} {port}"
    else
    case "$1" in
        mysql)
            check_isnotExist
            ;;
        redis)
            check_isnotExist
            ;;
        *)
            
         echo "usage: $0 {mysql|redis} {port}"
         exit -1
    esac
    fi
    

     

    11.find操作

    job_date=`date '+%Y'`

    find $dir/$job_date* -mtime +2 -type d -maxdepth 1 -exec rm -rf {} ;  --删除两天前文件

    find $dir/$job_date* -mtime +2 -type f ! -name "*txt*" -exec rm -rf {} ; --删除两天前不包括txt的文件

    12.获取cpu  内存  io

    us=`top -b -n1 | grep %Cpu | tail -1  | awk -F ":" '{print $2}' | awk -F "," '{print $1}' | awk -F " " '{print $1}'`
    sy=`top -b -n1 | grep %Cpu | tail -1  | awk -F ":" '{print $2}' | awk -F "," '{print $2}' | awk -F " " '{print $1}'`
    ni=`top -b -n1 | grep %Cpu | tail -1  | awk -F ":" '{print $2}' | awk -F "," '{print $3}' | awk -F " " '{print $1}'`
    id=`top -b -n1 | grep %Cpu | tail -1  | awk -F ":" '{print $2}' | awk -F "," '{print $4}' | awk -F " " '{print $1}'`
    wa=`top -b -n1 | grep %Cpu | tail -1  | awk -F ":" '{print $2}' | awk -F "," '{print $5}' | awk -F " " '{print $1}'`
    echo "===cpu使用占比统计==="
    echo "用户使用cpu%:" $us
    echo "系统使用cpu%:" $sy
    echo "用户优先级使用cpu%:" $ni
    echo "空闲占比cpu%:" $id
    echo "等待输入输出cpu%:" $wa
    
    total=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $1}'`
    used=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $2}'`
    free=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $3}'`
    shared=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $4}'`
    cache=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $5}'`
    available=`free -m | grep "Mem" | awk -F ":" '{print $2}'  |  awk -F " " '{print $6}'`
    echo "===内存使用统计==="
    echo "总分配内存(m):" $total
    echo "已使用内存(m):" $used
    echo "已使用内存占比(%):" `echo "scale=2;$used/$total*100" |bc`%
    echo "空闲内存(m)" $free
    echo "共享内存(m):" $shared
    echo "磁盘缓存(m):" $cache
    echo "可用内存(m):" $available
    
    #!/bash/sh
    
    set -e 
    
    root_total=`df -TH / | tail -1 | awk '{print $3}'`
    data_total=`df -TH /data | tail -1 | awk '{print $3}'`
    root_used=`df -TH / | tail -1 | awk '{print $6}'`
    root_used=`df -TH / | tail -1 | awk '{print $6}'`
    data_used=`df -TH /data | tail -1 | awk '{print $6}'`
    echo "===磁盘使用统计==="
    echo "根目录磁盘大小(G):" $root_total
    echo "根目录使用(%):" $root_used
    echo "data分区磁盘大小(G):" $data_total
    echo "data分区使用(%):" $data_used
    
    
    echo "===磁盘io输入/输出操作花费统计==="
    disk_name=($(cat /proc/diskstats | awk -F " " '{print $3}'))
    disk_time=($(cat /proc/diskstats | awk -F " " '{print $13/$14}'))
    number=1000
    for((i=0;i<${#disk_name[@]};i++))
    do
      for((j=0;j<${#disk_time[@]};j++))
      do
        if [[ $i -eq $j ]] 
        then
           echo "磁盘"${disk_name[i]}"分区输入/输出操作花费的时间是(%):" `echo "scale=2;${disk_time[j]}*100/1" |bc`%
        fi
      done
    done
    

    13. 限速

    #!/bin/bash
    set -e 
    
    speed=9mbit
    iplist=(10.0.0.10)
    
    sudo yum install net-tools -y
    sudo tc qdisc add dev eth0 root handle 1: htb r2q 1
    sudo tc class add dev eth0 parent 1: classid 1:1 htb rate $speed ceil $speed
    for ip in ${iplist[@]}
    do
      sudo tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip dst $ip flowid 1:1
    done
    sudo yum install iftop -y
    sudo iftop -i eth0
    

     14.时间计算

    #!/bin/bash
    
    #clean jobs
    
    set -e
    
    
    
    check_xxx(){
     xxx
    }
    
    a_list=($(xxx))
    
    for a_id in ${a_list[@]}
    do
        aa_id=$(xxx | awk -F '"' '{print $4}' | awk 'NR==1{print $1}')
        ab=$(xxx | grep "xxx" | grep -v "xxx" | awk -F '"' '{print $4}' | awk 'NR==1{print $1}' )
        cd xxx
        log_time=$(tail -1 INFO.log | awk -F' ' '{ print $1 " " substr($2,0,8)}' | sed 's/"//g')
        current_time=`date '+%Y-%m-%d %H:%M:%S'`
        days=$(python date_diff.py  $current_time $log_time | awk 'NR==1{print $1}')
        hours=$(python date_diff.py  $current_time $log_time | awk 'NR==2{print $1}')
        if [[ $days -eq 0 ]]
        then
            if [[ $hours -gt 2 ]]
            then
               xxx
            fi
        else
            xxx
        fi
    done
    
    
    
    # -*- coding: utf-8 -*-  
    
    import sys
    from datetime import datetime
    import time
    
    current_time=sys.argv[1]+" "+sys.argv[2]
    log_time=sys.argv[3]+" "+sys.argv[4]
    
    format = '%Y-%m-%d %H:%M:%S'
    a = datetime.strptime(log_time, format)
    b = datetime.strptime(current_time, format)
    t1 = time.mktime(a.timetuple()) * 1000 + a.microsecond / 1000
    t2 = time.mktime(b.timetuple()) * 1000 + b.microsecond / 1000
    a = t2-t1
    b = a/1000/3600
    c = int(b/24)
    d = int(b%24)
    print(c)
    print(d)
  • 相关阅读:
    解决RobotFramework的关键字不能高亮的问题
    使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
    通过Jekins执行bat脚本始终无法完成
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    [转]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    HDU 2686 MCMF
    HDU 4278 卡特兰,区间DP
    POJ 2985 名次树
    POJ 2531 深搜剪枝
    Uva 10061 进制问题
  • 原文地址:https://www.cnblogs.com/xinfang520/p/12760823.html
Copyright © 2020-2023  润新知