• 通过top命令发现cpu消耗高的应用
top - 23:23:45 up 132 days,  3:18,  1 user,  load average: 2.50, 2.62, 2.67
Tasks: 455 total,   3 running, 452 sleeping,   0 stopped,   0 zombie
%Cpu(s): 22.5 us, 31.5 sy,  0.0 ni, 45.8 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 15990752 total,   325208 free, 12212132 used,  3453412 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  2644488 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                                                                                  
 3945 root      20   0  108124  23420   3756 R 100.0  0.1   1:13.90 php                                                                                                                                                                                                      
 4056 root      20   0  108756  22204   3548 R 100.0  0.1   1:05.99 php     
  • 使用proc进入程序使用的数据目录
/proc/3945/cwd
  • 通过配置的env文件得出这个是一个消息推送的微服务
然后重启这个容器, 就好了, cpu也不再飙高了

  • 回退一个版本, 会删除本地最后一个版本(危险操作, 慎用)
git reset HEAD~1 && git reset --hard
  • 如果此刻再次执行git pull后, 会重新下载到最新的状态
  • 查看第N个提交的hash值, 当前值为1
vv=1;
git_hashcode=`git log -n $vv --pretty=format:"%H"`
v2=0;
for line in $git_hashcode
do
    declare -i v2=$v2+1;
    if [ "$v2" -eq "$vv" ]
    then
        echo $line;
    fi
done
  • 代理执行命令
git clone --config http.proxy="http://user:password@agent.com:1234"  https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

  • demo1, 支持待空格的文件拷贝, 不支持目录拷贝
#!/bin/bash
mkdir old_version

copy_from=`pwd`;
copy_to=`pwd`/old_version/;
for file in $copy_from/*;
    do
        cp -rfavx "$file" "$copy_to";
    done
# cp -rf "/home/bash/testgit/git1/111 - 副本 (5).txt" "/home/bash/testgit/git1/old_version/"
# exit;

  • 安装 zsh 软件包和 git
CentOS: yum -y install zsh git
Ubuntu:  sudo apt -y install zsh git
  • 克隆 oh-my-zsh
git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
  • 复制 .zshrc,修改命令提示符样式
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
ZSH_THEME="ys"
  • 修改 shell 类型
chsh -s /bin/zsh
[root@localhost ~]# chsh -s /bin/zsh
bash: chsh: 未找到命令
[root@localhost ~]# dnf -q provides '*/chsh'
util-linux-user-2.32.1-22.el8.x86_64 : libuser based util-linux utilities
仓库        :base
匹配来源:
文件名    :/etc/pam.d/chsh
文件名    :/usr/bin/chsh
文件名    :/usr/share/bash-completion/completions/chsh

通过上诉内容可以判断出, 软件包在util-linux-user中, 执行下面的安装就顺利安装好了
[root@localhost ~]# dnf install util-linux-user -y
[root@localhost ~]# chsh -s /bin/zsh
正在更改 root 的 shell。
shell 已更改。

注意: xshell客户端复制SSH渠道依旧是看到的依旧是原样, 需要重新连接就是zsh的shell了

# 如果需要改回去
➜  ~ bash
[root@localhost ~]# chsh -s /bin/bash
正在更改 root 的 shell。
shell 已更改
[root@localhost ~]# chsh -s /bin/bash
  • 如果不想修改, 可以通过执行zsh进入
[root@localhost ~]# zsh
➜  ~ 
  • 测试一下
➜  ~ lll
zsh: command not found: lll
修改配置
vi ~/.zshrc
添加下面内容
function lll(){
    (ls -la)
}
重载配置
source ~/.zshrc
执行自定义命令测试
➜  ~ lll
总用量 15128
dr-xr-x---.  8 root root    4096 7月   8 00:22 .
dr-xr-xr-x. 19 root root     250 7月   7 21:39 ..
...
  • 常用docker方法
function docker-exec() {
    (docker-compose exec $* bash)
    }
#重启docker 容器 修改yml文件后重载
function docker-reload() {
  (docker-compose down && docker-compose up -d $*)
}
#启动docker
function docker-start() {
  (docker-compose start $*)
}
#重启docker
function docker-restart() {
  (docker-compose restart $*)
}
#移除当前docker
function docker-remove(){
  (docker-compose down $*)
}
#构建启动docker
function docker-up() {
  (docker-compose up -d $* )
}
function docker-log() {
  (docker-compose logs $* )
}
#删除当前容器
function docker-rm(){
   (docker stop $1 | docker rm $1)
}
function docker-into(){
    (docker exec -it $* /bin/bash)
}

  • array_diff_assoc2
array_diff_assoc 在比对两个带key的数组时, 如果右边的数组没有对应的key, 左边对应的key是数组, 那没事, 不然就会转换左边的内容为字符串带来警告
function array_diff_assoc2($array, $array2)
{
    $d = [];
    foreach ($array as $key => $item) {
        if (array_key_exists($key, $array2)) {
            if (is_array($item)) {
                if (is_array($array2[$key])) {
                    if ($item != $array2[$key]) {
                        $d[$key] = $item;
                    }
                } else if (
                    (strpos($array2[$key], '[') === false 
                        && strpos($array2[$key], '{') === false)
                    || $item != json_decode($array2[$key], true)
                ) {
                    $d[$key] = $item;
                }
            } else if (is_array($array2[$key])) {
                if ($item != json_encode($array2[$key], 256)) {
                    $d[$key] = $item;
                }
            } else if ($item . '' != $array2[$key] . '') {
                $d[$key] = $item;
            }
        } else {
            $d[$key] = $item;
        }
    }
    return $d;
}
  • json_encode [php特殊环境下无法使用该方法的过度办法]
function json_encodes($arr)
{
    $parts = array();
    $is_list = false;
    //Find out if the given array is a numerical array
    if (is_array($arr)) {
        $keys = array_keys($arr);
        $max_length = count($arr) - 1;
        if (
            !is_null($keys)
            && array_key_exists('0', $keys)
            && ($keys [0] === 0)
            && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
            $is_list = true;
            for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
                if ($i != $keys [$i]) { //A key fails at position check.
                    $is_list = false; //It is an associative array.
                    break;
                }
            }
        }
        foreach ($arr as $key => $value) {
            if (is_array($value)) { //Custom handling for arrays
                if ($is_list)
                    $parts [] = json_encodes($value); /* :RECURSION: */
                else
                    $parts [] = '"' . $key . '":' . json_encodes($value); /* :RECURSION: */
            } else {
                $str = '';
                if (!$is_list)
                    $str = '"' . $key . '":';
                //Custom handling for multiple data types
                if (is_numeric($value) && $value === $value * 1) {
                    $str .= $value;
                } elseif ($value === false)
                    $str .= 'false'; //The booleans
                elseif ($value === true)
                    $str .= 'true';
                elseif ($value === null)
                    $str .= 'null';
                else
                    $str .= '"' . addslashes($value) . '"'; //All other things
                // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                if (is_string($str)) {
                    $str = str_replace(PHP_EOL, '\r\n', $str);
                }
                $parts [] = $str;
            }
        }
    }

    $json = implode(',', $parts);
    if ($is_list)
        return '[' . $json . ']'; //Return numerical JSON
    return '{' . $json . '}'; //Return associative JSON
}