常用命令 adduser 1 2 3 4 5 6 7 8 9 10 11 12 $ adduser eagle $ passwd eagle $ chmod u+w /etc/sudoers $ vim /etc/sudoers eagle ALL=(ALL) ALL $ chmod u-w /etc/sudoers $ su - eagle
awk 求和 1 2 3 4 $ echo '1' >> sum $ echo '2' >> sum $ echo '3' >> sum $ cat sum
1 $ cat sum | awk '{s+=$1} END {print s}'
1 $ awk '{s+=$1} END {print s}' sum
过滤 1 2 3 4 5 6 7 8 9 10 $ > test_awk.txt $ echo "a 1 3" >> test_awk.txt $ echo "b 1 2" >> test_awk.txt $ echo "c 2 1" >> test_awk.txt $ cat test_awk.txt | awk '{if($2>1) print}'
1 2 $ cat test_awk.txt | awk '{if($3>2) print}'
bash 1 2 $ bash -c "$(curl -L https://raw.githubusercontent.com/asdf2014/algorithm/master/first_commit.sh) "
cat 1 2 3 $ cat /etc/issue Yuzhouwan Group Enterprise Linux Server release 7.2
chown 1 2 3 4 $ chown -h superset:superset superset $ chown -R superset:superset superset-0.15.4
cp 1 2 3 4 5 6 7 8 $ cp -r -p -u -v ../* / $ cp ~/does_not_exist ~/somewhere 2>/dev/null | true && echo "always get here"
为了避免别名的影响,可以使用 /bin/cp 原生的命令,或者 unalias cp 消除别名
crontab 1 2 3 4 5 6 7 $ tail -f /var/log/cron $ crontab -e MAILTO=""
curl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ curl -H "Content-Type:application/json" -X POST --data '{"blog":"yuzhouwan"}' localhost:8080/api/query for (( i=1; i<=100; i++ )); do echo '[{"ts": "yuzhouwan_' `date "+%s" `'"}]' > id.data; curl -s -X POST -H "Content-Type: application/json" http://yuzhouwan:8081/api/put -d "@id.data" ; done $ gzip raw $ curl -X POST --data-binary "@raw.gz" --header "Content-Type: application/json" --header "Content-Encoding: gzip" http://yuzhouwan:8888/gzip_api $ curl cip.cc $ curl xxx --proxy "socks5://127.0.0.1:9876"
date 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 $ echo "`date -d '2017-04-21 10:00:00' +%s`000" $ date +%s $ date '+%Y-%m-%d %H:%M:%S' $ date "+%s%N" 1539065478653578171 $ date -R Sun, 12 Mar 2023 15:28:54 +0800 $ date -R -d '-1day' Sun, 12 Mar 2023 15:28:54 +0800 $ date -d@1545149326 Wed Dec 19 00:08:46 CST 2018 $ date -d@1543210000 "+%Y-%m-%d %H:%M:%S" 2018-11-26 13:26:40 $ date -r 1577808000 2020年 1月 1日 星期三 00时00分00秒 CST $ date -r 1577808000 "+%Y-%m-%d %H:%M:%S" 2020-01-01 00:00:00
df 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 $ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/systemvg-rootlv 7.9G 388M 7.1G 6% / tmpfs 3.9G 12K 3.9G 1% /dev/shm /dev/vda1 485M 39M 421M 9% /boot /dev/mapper/systemvg-homelv 30G 11G 18G 38% /home /dev/mapper/systemvg-optlv 30G 187M 28G 1% /opt /dev/mapper/systemvg-tmplv 2.0G 68M 1.9G 4% /tmp /dev/mapper/systemvg-usrlv 9.9G 2.7G 6.7G 29% /usr /dev/mapper/systemvg-varlv 6.0G 352M 5.3G 7% /var /dev/mapper/datavg-datalv 98G 4.1G 89G 5% /data $ ls -trl /dev/mapper/ crw-rw---- 1 root root 10, 58 May 27 2017 control lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-lv_swap -> ../dm-0 lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-optlv -> ../dm-4 lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-tmplv -> ../dm-5 lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-usrlv -> ../dm-6 lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-varlv -> ../dm-3 lrwxrwxrwx 1 root root 7 May 27 2017 systemvg-rootlv -> ../dm-1 lrwxrwxrwx 1 root root 7 Jun 1 2017 datavg-datalv -> ../dm-7 lrwxrwxrwx 1 root root 7 Jun 1 2017 systemvg-homelv -> ../dm-2
diff 1 2 3 4 5 6 7 8 9 10 11 $ echo "a" > a $ echo "b" > b $ diff a b 1c1 < a --- > b
du 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 $ du -h /home/ --max-depth=1 3.1G /home/eagle 40.8G /home/ $ du -sh * 64K logs 276M software 28K zkdata $ du -sm * | sort -nr $ ll -h 总用量 546M -rw-rw-r-- 1 zookeeper zookeeper 41M 8月 31 10:08 zookeeper.log -rw-rw-r-- 1 zookeeper zookeeper 101M 8月 31 00:06 zookeeper.log.1 -rw-rw-r-- 1 zookeeper zookeeper 101M 8月 29 11:39 zookeeper.log.2 -rw-rw-r-- 1 zookeeper zookeeper 101M 8月 27 16:01 zookeeper.log.3 -rw-rw-r-- 1 zookeeper zookeeper 101M 8月 25 20:38 zookeeper.log.4 -rw-rw-r-- 1 zookeeper zookeeper 101M 8月 24 00:34 zookeeper.log.5 -rw-rw-r-- 1 zookeeper zookeeper 4.2M 8月 31 09:35 zookeeper.out
echo 1 2 3 4 5 6 7 8 9 $ echo -e "yuzhouwan.com\c" >> blogs.txt $ echo "scale=2;1234/1000" | bc 1.23 $ res=$(echo "scale=2;1234/1000" | bc) $ echo $res 1.23
find 1 2 3 4 5 $ find / -type f -print0 | xargs -0 du -h | sort -rh | head -n 10 $ find . -name "*.log" -delete
firefox 1 2 $ yum install firefox $ firefox yuzhouwan.html
grep 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 $ grep -E 'bin|etc' $ egrep 'bin|etc' $ awk '/bin|etc/' $ grep bin | grep etc $ grep -i BIN $ grep -w bin $ grep -C 1 bin $ zkServer.sh status |& grep Mode Mode: follower $ grep \$ $ vim yuzhouwan.txt 1 2 3 4 5 6 $ cat yuzhouwan.txt | grep -A 2 3 3 4 5
hostname 1 2 3 $ hostname -i 10.10.10.1
iostat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 $ iostat -x 1 10 Linux 2.6.32-504.3.3.el6.centos.plus.x86_64 (yuzhouwan01.com) 01/19/2018 _x86_64_ (24 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 17.28 0.00 2.02 0.08 0.00 80.62 Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util sdb 0.01 159.15 2.51 9.62 493.46 1350.14 151.96 0.02 1.77 0.28 0.34 sdd 0.01 162.24 2.49 9.69 493.13 1375.44 153.42 0.02 1.85 0.28 0.34 sdc 0.01 162.57 2.49 9.83 487.72 1379.15 151.58 0.02 2.02 0.27 0.34 sde 0.01 161.54 2.54 9.80 500.08 1370.68 151.61 0.02 1.87 0.28 0.34 sdg 0.01 162.06 2.53 9.83 498.98 1375.10 151.64 0.02 1.93 0.28 0.34 sdf 0.01 163.34 2.53 9.74 496.43 1384.64 153.22 0.02 1.92 0.28 0.35 sdh 0.01 161.44 2.56 9.74 502.61 1369.43 152.20 0.02 1.90 0.28 0.34 sdi 0.01 162.58 2.54 9.78 498.87 1378.82 152.48 0.02 1.99 0.28 0.35 sda 0.29 83.48 0.29 146.49 14.22 1146.37 7.91 0.04 0.28 0.05 0.69 sdj 0.01 164.20 2.60 9.77 512.11 1391.74 153.90 0.02 1.97 0.28 0.35 sdk 0.01 161.76 2.60 9.70 505.17 1371.69 152.56 0.03 2.04 0.28 0.35 sdl 0.01 168.31 2.50 9.84 490.59 1425.20 155.30 0.03 2.57 0.28 0.35 sdm 0.01 162.34 2.52 9.69 486.76 1376.20 152.68 0.02 1.97 0.28 0.34 dm-0 0.00 0.00 0.02 0.31 0.70 2.45 9.64 0.00 6.04 0.43 0.01 dm-1 0.00 0.00 0.39 0.42 3.10 3.32 8.00 0.00 3.42 0.27 0.02 dm-2 0.00 0.00 0.10 173.34 7.42 1035.66 6.01 0.02 0.04 0.03 0.55 dm-3 0.00 0.00 0.01 7.47 0.22 59.05 7.93 0.00 0.56 0.07 0.05 dm-4 0.00 0.00 0.00 4.10 0.00 32.78 8.00 0.00 0.56 0.04 0.02 dm-5 0.00 0.00 0.04 0.81 1.26 6.51 9.07 0.00 0.59 0.17 0.01 dm-6 0.00 0.00 0.01 0.82 1.51 6.59 9.68 0.00 2.02 0.16 0.01
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 $ wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 $ mv jq-linux64 ~/software/ $ cd ~/software/ $ chmod +x jq-linux64 $ ln -s jq-linux64 jq $ vim ~/.bashrc alias jq='/home/connect/software/jq' $ source ~/.bashrc $ curl localhost:8083/ | jq { "version" : "0.11.0.0-cp1" , "commit" : "5cadaa94d0a69e0d" } $ jq ".element" $ jq ".element[0]" $ jq -c
jstack
jstat 1 2 3 4 5 6 7 8 9 10 11 12 13 $ jstat -gcutil 24909 1000 S0 S1 E O P YGC YGCT FGC FGCT GCT 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 98.56 6.38 46.86 2 0.147 0 0.000 0.147 40.77 0.00 99.16 6.38 46.87 2 0.147 0 0.000 0.147
lsblk
1 2 3 4 5 6 NAME ROTA vda 1 vdb 1
lsof 1 2 3 4 5 6 $ lsof -p <pid> $ lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr
nc 1 2 3 4 5 6 7 8 9 $ nc -l 8080 $ nc -l 8080 $ nc localhost 8080
nmon 生成 nmon 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 $ nmon_x86_64_centos6 -h $ /nmon/nmon_x86_64_rhel6 -f -N -m /nmon -s 60 -c 1440 $ /nmon/nmon_x86_64_centos6 -f -N -m /nmon -s 60 -c 1440 $ sort yuzhouwan-prd3_170831_0001.nmon > yuzhouwan-prd3_170831_0001.csv
系统资源实时监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 $ /nmon/nmon_x86_64_centos6 lnmonq14g-------------------Hostname=yuzhouwan-prd3-Refresh= 2secs ---11:04.35-----------| | CPU +-------------------------------------------------------------------------+ | |100%-| | | | 95%-| | | | 20%-| | | | 15%-| | | | 10%-| ss | | | 5%-|UUUUsUUUUsUUsUUUUwUUssUssUUssUUUsUUsUUUUUsU| | | +--------------------User---------System----+----Wait---------------------+ | | Memory Stats --------------------------------------------------------------------------| | RAM High Low Swap Page Size=4 KB | | Total MB 129013.3 -0.0 -0.0 10240.0 | | Free MB 584.1 -0.0 -0.0 10240.0 | | Free Percent 0.5% 100.0% 100.0% 100.0% | | MB MB MB | | Cached= 76656.7 Active= 54649.3 | | Buffers= 4908.1 Swapcached= 0.0 Inactive = 68809.5 | | Dirty = 131.6 Writeback = 0.0 Mapped = 75.1 | | Slab = 4059.3 Commit_AS = 47663.8 PageTables= 96.6 | | Network I/O ---------------------------------------------------------------------------| |I/F Name Recv=KB/s Trans=KB/s packin packout insize outsize Peak->Recv Trans | | lo 1044.0 1044.0 335.5 335.5 3186.8 3186.8 8639.4 8639.4 | | eth0 5262.7 1168.7 5999.0 2762.1 898.3 433.3 10531.6 43312.1 | | eth1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 | |---------Warning: Some Statistics may not shown-----------------------------------------|
nohup 1 2 3 4 5 6 7 8 9 10 11 12 13 $ nohup sleep 1000 & [1] 30408 $ jobs [1]+ Running nohup sleep 1000 & $ fg %1 nohup sleep 1000 $ jobs $ nohup bash yuzhouwan.sh >/dev/null 2>&1 &
make 1 2 3 4 5 $ make -j<thread_num> $ cat /proc/cpuinfo | grep processor | wc -l
mount 重命名挂载点 1 2 3 4 5 6 7 $ sudo su root $ mkdir -p /yuzhouwan $ vim /etc/fstab /dev/mapper/vg_os-lv_app /yuzhouwan ext4 defaults 1 2 $ umount /old $ mount /yuzhouwan
locale 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ locale LANG=en_US.UTF-8 LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=
tar 1 2 3 4 $ tar zcvf gc.tar.gz gc $ tar zxvf gc.tar.gz
top 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ top -Hp <pid> $ printf '%x\n' <thread_pid> $ jstack <pid> | less $ top -b -n 1
tree 1 2 3 4 5 6 $ yum install tree -y $ tree -L 1 / / ├── bin ├── usr └── var
rpm 1 $ rpm -i --badreloc --relocate /usr/java=/home/eagle/software/java jdk-7u80-linux-x64.rpm
1 2 $ rsync -avuz -e ssh eagle/ root@eagle:/home/eagle
sed 1 2 $ sed -in-place -e 's/blog/yuzhouwan.com/g' yuzhouwan.json
scp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ scp yuzhouwan root@192.168.1.1:/ root@192.168.1.1's password: yuzhouwan 0% 11MB 1.3MB/s 5:24:10 ETA^Z # Ctrl+Z 暂停 scp 任务 [1]+ Stopped scp yuzhouwan root@192.168.1.1:/ # 查看任务已经被暂停 $ jobs [1]+ Stopped scp yuzhouwan root@192.168.1.1:/ # 使得其在后台恢复运行 $ bg %1 [1]+ scp yuzhouwan root@192.168.1.1:/ & # 已恢复运行 $ jobs [1]+ Running scp yuzhouwan root@192.168.1.1:/ & # 忽略 HUP 信号(终端断线的中断信号) $ disown -h %1 # 退出 $ exit # 重新登录后,查看 scp 后台任务是否正常运行 $ ps -ef | grep scp root 6189 1 0 18:04 ? 00:00:00 scp yuzhouwan root@192.168.1.1:/ root 6190 6189 0 18:04 ? 00:00:00 /usr/bin/ssh -x -oForwardAgent=no -oPermitLocalCommand=no -oClearAllForwardings=yes -l root -- 192.168.1.1 scp -t / root 7723 7643 0 18:05 pts/3 00:00:00 grep --color=auto scp
split 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 $ echo "1" >> yuzhouwan $ echo "2" >> yuzhouwan $ echo "3" >> yuzhouwan $ cat yuzhouwan 1 2 3 $ split -l 1 yuzhouwan tmp_ $ split -b 2 yuzhouwan tmp_ $ ll tmp_* -rw-r--r-- 1 root root 2 Aug 2 19:35 tmp_aa -rw-r--r-- 1 root root 2 Aug 2 19:35 tmp_ab -rw-r--r-- 1 root root 2 Aug 2 19:35 tmp_ac $ cat tmp_aa 1 $ cat tmp_ab 2 $ cat tmp_ac 3 $ cat tmp_* > yuzhouwan_new $ cat yuzhouwan_new 1 2 3
ssh 1 2 3 4 5 6 7 8 $ ssh bj@192.168.1.101 "df -h" $ ssh -i ~/.ssh/id_rsa bj@192.168.1.101 "df -h" $ ssh bj@192.168.1.101 "pwd; ls"
sz 1 2 3 4 5 6 7 8 $ yum install lrzsz -y $ sz <file name> -e, --escape Force sender to escape all control characters; normally XON, XOFF, DLE, CR-@-CR, and Ctrl-X are escaped.
ping 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ ping yuzhouwan01 PING yuzhouwan01.com (192.168.1.101) 56(84) bytes of data. 64 bytes from yuzhouwan01.com (192.168.1.101): icmp_seq=1 ttl=64 time=0.055 ms 64 bytes from yuzhouwan01.com (192.168.1.101): icmp_seq=2 ttl=64 time=0.066 ms 64 bytes from yuzhouwan01.com (192.168.1.101): icmp_seq=3 ttl=64 time=0.064 ms 64 bytes from yuzhouwan01.com (192.168.1.101): icmp_seq=4 ttl=64 time=0.083 ms ^C --- yuzhouwan01.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3443ms rtt min/avg/max/mdev = 0.055/0.067/0.083/0.010 ms $ ping -c 4 yuzhouwan01 -W 5
ps 1 2 3 4 5 6 $ ps -eo pid,lstart,etime | grep <pid> $ ps -e -o pid,rss
ulimit 1 2 3 4 5 6 7 8 $ ulimit -n 1024 $ ulimit -HSn 4096
uname 1 2 3 4 5 6 7 8 $ uname -a Linux yuzhouwan 2.6.32-504.3.3.el6.centos.plus.x86_64 $ uname -r 2.6.32-504.3.3.el6.centos.plus.x86_64
umask 1 2 3 4 5 6 7 8 9 10 11 $ umask 0022 $ umask -S u=rwx,g=rx,o=rx $ umask 0022
vim 按照单词跳转
撤销
翻页
退出并保存
wget 从 ftp 服务器下载文件 1 $ wget -nd -m --ftp-user=<user> --ftp-password=<password> ftp://<ftp_server>/zookeeper/zookeeper-3.4.6.4.tar.gz
输出到控制台 1 2 3 4 5 6 $ wget -q -O- <url> $ wget -qO- <url>
Tips: 另外还有一个好处,wget 可以实现跳转。以 Apache Druid 为例,用 curl 命令去访问 http://standby-overlord:8090/druid/indexer/v1/runningTasks
会因为 overlord 是 standby 结点,导致调用接口失败。而如果用 wget 命令则可以自动跳转到 active 结点,同时再配合使用 -qO-
命令,就能达到和 curl
命令一样,将接口调用结果直接输出到控制台的效果(当然 curl -L
也可以实现)
xargs 1 2 $ jps -ml | grep yuzhouwan | awk '{print $1}' | xargs -i jstat -gcutil {} 1000
标准 I/O 1 2 3 4 $ ./commands.sh 1>/dev/null 2>./error.log $ ./commands.sh 1>/dev/null 2>&1
Shell 编程 .bashrc
dirname
cut string 1 2 3 arr="[leader, election, zookeeper]" arr=`echo ${arr:1:${#arr} -2} `
command 1 2 3 4 command -v nc >/dev/null 2>&1 || { echo >&2 "I require nc but it's not installed!" ; exit 1; }
for loop 遍历文件 1 2 3 for line in `cat yuzhouwan.txt`; do echo $line done
进阶用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 arr="leader, election, zookeeper" OLD_IFS="$IFS " IFS=$", " arr=(${arr} ) for a in ${arr[@]} ; do echo ${a} done IFS="$OLD_IFS " sorted_num=`echo ${sorted} | wc -l` if [ ${sorted_num} -gt ${top_n} ]; then sorted_num=${top_n} fi json="{" for (( i=1; i<=${sorted_num} ; i++ )); do line=`echo "${sorted} " | sed -n "${i} p" ` json="${json} `echo ${line} | awk '{print $1 }'`: `echo ${line} | awk '{print $2 }'`" if [ ${i} -lt ${sorted_num} ]; then json="${json} , " fi done json="${json} }" for i in $(seq 1 2 10); do echo "skip by 2 step, value is $i " ; done
function 1 2 3 4 5 6 7 8 function f () { echo "$1 .$2 " } f "yuzhouwan" "com"
if 1 2 3 4 5 6 7 8 9 10 11 if [[ ${context} _x == "_x" ]]; then echo "OK" fi if [[ $(echo "${origin_cost_time} >= ${compact_cost_time} " |bc) -eq 1 ]]; then echo "True" >> time.txt else echo "False" >> time.txt fi
paste 1 2 3 $ cat c | awk '{print $1}' | paste -sd+ - | bc 160
readlink 1 2 3 $ readlink -f logs/zookeeper.log /home/zookeeper/logs/zookeeper.log
sed 替换 1 2 3 zk_con=" /192.168.1.1:35632[1](queued=0,recved=146,sent=146,sid=0x25dd0c02a0a02a7,lop=PING,est=1503994626336,to=40000,lcxid=0x0,lzxid=0xffffffffffffffff,lresp=1503996561355,llat=0,minlat=0,avglat=0,maxlat=3)" zk_con=`echo $zk_con | sed 's/:.*//g' | sed 's/.*\///g' `
删除匹配行 1 2 $ sed -e '/yuzhouwan/d' txt
删除空白行 1 2 3 4 $ sed -i 's/^$//g' log $ sed -i '/^\s*$/d' log
sort 指定某一列进行排序 1 2 3 4 5 6 7 8 9 10 sorted=`echo ${result} | sort -k1 -n`
指定多个列进行排序 1 2 3 4 5 6 7 8 $ > test_sort.txt $ echo "a 1 3" >> test_sort.txt $ echo "b 1 2" >> test_sort.txt $ echo "c 2 1" >> test_sort.txt cat test_sort.txt
1 2 $ cat test_sort.txt | sort -k2,2 -k3,3 -V
1 2 $ cat test_sort.txt | sort -k3,3 -k2,2 -V
uniq 1 2 3 4 echo -e "1\n1\n0" | uniq -c
exit 0
实用技巧 确定操作系统类型 1 $ cat /etc/redhat-release
1 2 CentOS Linux release 7.9.2009 (Core)
IFS 设置 Shell 脚本中 IFS 变量全称 I nternal F ield S eprator,内部域分隔符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ echo $IFS $ echo "$IFS " | od -b 0000000 040 011 012 012 0000004 pids=`ps -ef | grep "${PROCESS_NAME} " | grep -v grep | grep -v "${SELF_NAME} " | awk '{print $2}' ` IFS=$'\n' read -rd '' -a pids <<<"$pids " echo "pids: ${pids} " for pid in "${pids} " ; do echo "kill -9 ${pid} " kill -9 "${pid} " done
每秒执行一次 1 $ while true ; do sleep 1; <command >; done
批量后台运行 1 2 $ for i in $(seq 3); do echo $i ; sleep 2 & done
1 2 3 4 5 6 7 8 9 10 1 [2] 44066 2 [3] 44067 3 [4] 44068 [3] - 44067 done sleep 2 [4] + 44068 done sleep 2 [2] + 44066 done sleep 2
1 2 3 $ for i in $(seq 3); do echo $i ; (sleep 2 &) ; done
自启动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ vim keep_alive.sh while true ; do process_num=`ps -ef | grep -v grep | grep opentsdb | wc -l` echo ${process_num} if [[ ${process_num} -gt 0 ]]; then echo "health" else cd /usr/local/opentsdb nohup ./build/tsdb tsd >/dev/null 2>&1 & fi sleep 10; done $ nohup sh keep_alive.sh >/dev/null 2>&1 &
获得当前机器的资源使用情况 CPU 1 2 idle_cpu_percent=`top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "." ` used_cpu_percent=$(echo "100-${idle_cpu_percent} " | bc)
Memory 1 2 3 4 5 free_m=`free -m` free_m_detail=`echo ${free_m} | sed 's/.*Mem://g' | sed 's/-\/+.*//g' ` total_mem=$(echo "${free_m_detail} " | awk '{print $1}' ) used_mem=$(echo "${free_m_detail} " | awk '{print $2}' ) used_mem_percent=$[${used_mem} * 100 / ${total_mem} ]
Disk 空间使用率 1 used_disk_percent=`df -P | grep "home" | awk '{print $5}' `
I/O 使用率 1 2 3 virtual_device=`df -h | grep "home" | awk '{print $1}' | cut -f 4 -d '/' ` device=`ls -trl /dev/mapper/ | grep ${virtual_device} | sed 's/.*\///g' ` device_io=`iostat -x 1 1 | grep ${device} | awk '{print $12}' `
Network 获得当前机器的 IP 1 2 3 4 now_ip=`ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2}' | sed 's/.*://g' ` if [ "${now_ip} " == "" ]; then now_ip=`hostname -i | awk '{print $1}' ` fi
从机器列表中获取目标 IP 1 2 3 4 5 aim_ip=`cat ${zk_home} /conf/zoo.cfg | grep server | sed 's/.*=//g' | cut -d ':' -f 1 | grep -v ${now_ip} | tail -1` if [ "${aim_ip} " == "" ]; then aim_ip="127.0.0.1" fi
网络延迟和丢包率 1 2 3 ping_result=`ping -c 4 ${aim_ip} -W 5` ping_packet_loss=`echo ${ping_result} | sed 's/.*received, //g' | sed 's/ packet loss.*//g' ` ping_avg_ms=`echo ${ping_result} | sed 's/.*rtt //g' | awk '{print $3}' | cut -d '/' -f 2`
通用工具 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 $ wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate $ unzip tsar.zip $ cd tsar-master/ $ yum install gcc -y $ make $ make install $ tsar -l -i 1 Time ---cpu-- ---mem-- ---tcp-- -----traffic---- --vda--- --vda1-- ---load- Time util util retran bytin bytout util util load1 09/04/19-19:59:44 0.00 0.71 0.00 126.00 222.00 0.00 0.00 0.16 09/04/19-19:59:45 0.02 0.71 0.00 66.00 338.00 0.00 0.00 0.16 09/04/19-19:59:46 0.00 0.71 0.00 66.00 242.00 0.00 0.00 0.16
从 JVM 进程中,找到占用 CPU 最高的 Java 线程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ top -p 15108 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 15133 druid 20 0 24.9g 1.1g 7776 S 244.9 0.9 0:07.36 java 15108 druid 20 0 24.9g 1.1g 7776 S 0.0 0.9 0:00.57 java 15114 druid 20 0 24.9g 1.1g 7776 S 0.0 0.9 0:08.96 java 15115 druid 20 0 24.9g 1.1g 7776 S 0.0 0.9 0:07.26 java 15116 druid 20 0 24.9g 1.1g 7776 S 0.0 0.9 0:07.25 java 15117 druid 20 0 24.9g 1.1g 7776 S 0.0 0.9 0:07.19 java $ printf 0x%x 15133 0x3b1d $ jstack 15108 | grep -A 10 3b1d "qtp2028042905-211-acceptor-3@41374390-ServerConnector@2faa55bb{HTTP/1.1}{0.0.0.0:8090}" java.lang.Thread.State: RUNNABLE at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) - locked <0x00000007122b1fa0> (a java.lang.Object) at org.eclipse.jetty.server.ServerConnector.accept(ServerConnector.java:377) at org.eclipse.jetty.server.AbstractConnector$Acceptor .run(AbstractConnector.java:500) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620) at org.eclipse.jetty.util.thread.QueuedThreadPool$3 .run(QueuedThreadPool.java:540) at java.lang.Thread.run(Thread.java:745)
Windows 相关 Windows 下执行 shell 脚本 1 2 3 4 5 6 7 $ doskey bash="%GIT_HOME%\bin\bash.exe" $* $ doskey sh="%GIT_HOME%\bin\sh.exe" $* $ bash shell.sh $ doskey np=D:\apps\Notepad++\notepad++.exe $*
Cmd 设置 Proxy 1 2 3 4 $ set http_proxy=http://your_proxy:your_port $ set http_proxy=http://username:password@your_proxy:your_port $ set https_proxy=https://your_proxy:your_port $ set https_proxy=https://username:password@your_proxy:your_port
Cygwin 安装 在官网 找到 setup-x86_64.exe 安装包,下载并执行(安装的过程中,如果提示有冲突,选择 contiune
而不能是 retry
,否则,将会全部重新开始)
使用 1 2 3 $ cd /cygdrive/e $ cd e:/
防火墙 CentOS CentOS 6 1 2 3 4 5 6 7 $ service iptable status $ servcie iptables stop $ chkconfig iptables off
CentOS 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ firewall-cmd --state $ systemctl list-unit-files | grep firewalld.service $ systemctl start firewalld.service $ systemctl restart firewalld.service $ systemctl stop firewalld.service $ systemctl status firewalld.service $ systemctl is-enabled/enable/disable firewalld.service
Ubuntu 1 2 3 4 5 6 7 $ ufw enable $ ufw disable $ apt-get remove iptables
优化实战 关闭 Swappiness 1 2 3 4 5 6 7 8 9 10 $ cat /proc/sys/vm/swappiness $ vim /etc/sysctl.conf vm.swappiness=10
控制 Overcommit 1 2 3 4 5 $ cat /proc/sys/vm/overcommit_memory
禁用透明巨页 Page Size 1 2 $ getconf PAGE_SIZE 4096
Huge Page Size 1 2 $ cat /proc/meminfo | grep Hugepagesize Hugepagesize: 2048 kB
Transparent Huge Pages 1 2 3 4 5 6 7 $ cat /sys/kernel/mm/transparent_hugepage/enabled [always] madvise never $ echo never > /sys/kernel/mm/transparent_hugepage/enabled $ echo never > /sys/kernel/mm/transparent_hugepage/defrag
清理 Cache 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 $ free -g && sync && echo 3 > /proc/sys/vm/drop_caches && free -g total used free shared buffers cached Mem: 3833 3726 106 0 107 272 -/+ buffers/cache: 3346 486 Swap: 10239 0 10239 total used free shared buffers cached Mem: 3833 3345 487 0 1 31 -/+ buffers/cache: 3312 520 Swap: 10239 0 10239 $ echo 1 > /proc/sys/vm/drop_caches $ echo 2 > /proc/sys/vm/drop_caches $ echo 3 > /proc/sys/vm/drop_caches $ echo 0 > /proc/sys/vm/drop_caches
增大预留空闲内存 1 2 3 4 5 6 7 $ cat /proc/sys/vm/min_free_kbytes 90112 $ vim /proc/sys/vm/min_free_kbytes 1048576
系统架构 Cache & Buffer 定义 在 Linux 的内存管理中,Cache (Page Cache)属于 页面缓存,Buffer (Buffer Cache)则属于 缓冲区缓存。前者,针对 页(Page)内存进行管理,后者,则针对 块(Block)内存进行管理
分析 Cache 安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $ yum install perl-Inline perl-Parse-RecDescent -y $ wget https://github.com/david415/linux-ftools/archive/master.zip $ unzip linux-ftools-master.zip $ cd linux-ftools-master $ chmod 777 configure $ ./configure --prefix=/usr/local/ && make -j2 && make -j2 install $ ln -sf /usr/bin/aclocal /usr/bin/aclocal-1.10 $ ln -sf /usr/bin/automake /usr/bin/automake-1.10 $ mkdir -p /tmp/fincore $ yum reinstall --downloadonly --downloaddir=/tmp/fincore perl-Inline perl-Parse-RecDescent -y $ ll /tmp/fincore -rw-r--r-- 1 root root 151484 Jul 22 2010 perl-Inline-0.46-1.el6.noarch.rpm -rw-r--r-- 1 root root 193348 Sep 25 2011 perl-Parse-RecDescent-1.965-1.el6.noarch.rpm
编码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #!/bin/sh command -v fincore >/dev/null 2>&1 || { echo >&2 "I require fincore but it's not installed. Try install..." ; exit 1; } pids_tmp=/tmp/cache.pids cache_tmp=/tmp/cache.files fincore_tmp=/tmp/cache.fincore result_tmp=/tmp/result.fincore echo "The top 3 cache pid:" ps -e -o pid,rss | sort -nk2 -r | grep -vw 1 | head -3 | awk '{print $1}' > ${pids_tmp} echo -e "`cat ${pids_tmp} `\n" for pid in `cat ${pids_tmp} `; do ps -ef | grep ${pid} done echo -e "\n" if [ -f ${cache_tmp} ]; then rm -f ${cache_tmp} fi while read line; do echo "Pid: ${line} , Files:" file=`lsof -p ${line} 2>/dev/null | awk '{print $9}' ` echo "${file} " >> ${cache_tmp} echo -e "${file} \n" | grep -vw "NAME" done <${pids_tmp} if [ -f ${fincore_tmp} ]; then rm -f ${fincore_tmp} fi for i in `cat ${cache_tmp} `; do if [ -f ${i} ]; then echo ${i} >> ${fincore_tmp} fi done fincore --pages=false `cat ${fincore_tmp} | grep -v /usr/sbin/sshd | grep -v /lib64/ld-2.12.so | grep -v /bin/bash | grep -v /bin/login` > ${result_tmp} echo "filename size total pages cached pages cached size cached percentage" len=`cat ${result_tmp} | wc -l` cat ${result_tmp} | tail -$(echo "${len} -1" | bc) | sort -nk4 -rexit 0
Tips: Full code is here .
System Tap 依赖包太多,线上环境不适合
卡在了安装的第一步 go get golang.org/x/sys/unix
…
回收 Cache 操作 具体操作,请看上文 “优化实战 - 清理 Cache ” 部分
成本 清理 Cache 之前,需要保证 Cache 中的数据,和对应文件中的数据一致,才能对 Cache 进行释放。因此,清除 Cache 之前,内核会检查 Cache 数据与对应磁盘文件数据一致性,并将不一致的 Cache 写回磁盘。这一过程,将消耗大量的磁盘 I/O 资源
无法被回收的情况 Linux 提供一种 “临时”文件系统(重启之后数据不会被保存),拿出一部分的内存空间当做文件系统使用,可以动态调整文件系统的大小,并且相比磁盘文件系统的读写速度,会快上很多倍。在 tmpfs 系统里面的内存,是不会被回收的,除非 tmpfs 里面的文件被删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 $ ll -h /dev/shm total 76K -rw-r--r-- 1 root root 46 Aug 13 11:05 10694 -r-------- 1 root root 65M May 25 14:43 pulse-shm-17791060 -r-------- 1 root root 65M May 25 14:43 pulse-shm-31652098 $ mkdir /tmp/tmpfs $ mount -t tmpfs -o size=3G none /tmp/tmpfs/ $ df -h Filesystem Size Used Avail Use% Mounted on tmpfs 63G 0 63G 0% /dev/shm none 3.0G 0 3.0G 0% /tmp/tmpfs $ free -g total used free shared buffers cached Mem: 125 79 46 0 0 0 -/+ buffers/cache: 79 46 Swap: 0 0 0 $ dd if =/dev/zero of=/tmp/tmpfs/yuzhouwan bs=1G dd : writing '/tmp/tmpfs/yuzhouwan' : No space left on device 4+0 records in 3+0 records out 3221225472 bytes (3.2 GB) copied, 2.57476 s, 1.3 GB/s $ free -g total used free shared buffers cached Mem: 125 83 42 0 0 3 -/+ buffers/cache: 80 45 Swap: 0 0 0 $ echo 3 > /proc/sys/vm/drop_caches $ free -g total used free shared buffers cached Mem: 125 84 41 0 0 3 -/+ buffers/cache: 80 44 Swap: 0 0 0 $ rm /tmp/tmpfs/yuzhouwan $ free -g total used free shared buffers cached Mem: 125 86 39 0 0 0 -/+ buffers/cache: 85 40 Swap: 0 0 0
ipcs 进程间通信(IPC)方式,只需要在 “输入文件” 到 “共享内存区” 之间进行两次数据的拷贝。但是,如果通过 shmget
创建完共享内存之后,没有在结束的时候,使用 shmctl
删除共享内存,那么,这块内存将一直无法被清理出去
1 2 3 $ ipcs -m $ ipcrm -m <shmid>
mmap 进程之间还可以通过 mmap
方法,映射同一个普通文件,来实现共享内存。普通文件被映射到进程地址空间后,进程可以像访问普通内存一样对文件进行访问,不必再调用 read
/ write
方法。同样地,如果没有在进程退出时,使用 munmap
方法解除映射关系,也会存在共享内存无法被清除的情况
资源 Book
群名称
群号
人工智能(高级)
人工智能(进阶)
大数据
算法
数据库