常用运维操作

一、日常操作

1.1 查看端口

查看机器开放哪些TCP端口。

$ netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2281/sshd
tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      16856/zabbix_agentd

1.2 查找服务

# 找到进程 或者TOP找到,但可能看不到完整的文件路径
$ lsof -i :5600
# 查看执行程序
$ ls -l /proc/4643/exe 
# 查看执行的文件路径
$ ls -lh /proc/12586/cwd


$ pwdx 1933
1933: /usr/local/cloudmonitor/wrapper/bin

二、磁盘报警

2.1 查找大文件

$ df -h
$ du -h / --max-depth=1

2. 2 清除超过30天的日志

$ find /data/logs -type f -name "*.log.*" -ctime +30 -exec rm -rf {} \; 
$ find /data/logs -type f -name "*.log.*" -ctime +30 -print0| xargs -0 rm

三、数据库

3.1 Mysql批量杀进程

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 3101;             |
| KILL 2946;             |
+------------------------+
2 rows in set (0.00 sec)

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
Query OK, 2 rows affected (0.00 sec)

mysql> source /tmp/a.txt;
Query OK, 0 rows affected (0.00 sec)

通过Shell删除

#! /bin/bash
for i in `mysql -uroot -pxxx -Bse "show processlist" | grep -v "show processlist" | awk '{print $1}'`
do 
    mysql -uroot -pxxx -e "kill $i"
done

四、日常操作

4.1 文件切分与合并

文件合并

$ cat *.csv > merge.csv
$ cat a.csv b.csv c.csv > abc.csv

按大小切割,每100M切割

$ split -b 100m filename

按行数切割,每100w行切割,并带上前缀

$ split -l10000000 pc.txt i_

合并:cat x* > pc.txt

4.2 统计文本中某一列的和

$ cat test.log | awk -F '\t' '{sum+=$2}END{print sum}'
$ cat test.log | awk -F '\t' '{sum1+=$2;sum2+=$3}END{print sum2*100/sum1}'

4.3 查询外网IP

$ curl ifconfig.me
$ curl cip.cc
$ curl myip.ipip.net
-- EOF --
最后更新于: 2024-08-17 14:44
发表于: 2018-09-22 22:22
标签: 运维