Linux系统管理之技巧

系统 Linux 系统运维
项目完成了一个阶段性的任务,在这里小结一下几个小技巧。

 [[186609]]

【引自年年歳歲的博客】项目完成了一个阶段性的任务,在这里小结一下几个小技巧:

1. 授权研发人员权限越级(sudo)

  1. $ cat public 
  2. User_Alias A4 = public 
  3. A4       ALL=(ALL)       NOPASSWD: /usr/bin/vim,/bin/cat,/usr/bin/tail  #这里你自己定义 
  4. $ ansible all -m copy -a 'src=public dest=/etc/sudoers.d' -s  

2. 批量memcached清除缓存(flush_all)

  1. echo 'flush_all' | nc -z -w 1 $IP $PORT 

如果有很多,可以向下罗列,批量清除,如果你觉得很low,可以使用数组declare -a,或者循环while,

写python当然也行,附上自己的过程,请参考:

  1. $ cat clear_memcached.py  
  2. from pymemcache.client.base import Client 
  3. import sys 
  4.   
  5. def read_log(path): 
  6.     with open(path) as f: 
  7.         yield from f 
  8.   
  9. def HP(path): 
  10.     for line in read_log(path): 
  11.         ret = line.strip().split() 
  12.         ret[1] = int(ret[1]) 
  13.         ret = tuple(ret) 
  14.         print(ret) 
  15.         yield ret 
  16.           
  17. def clear_mem(path): 
  18.     for hp in HP(path): 
  19.         c.flush_all() 
  20.         c.close() 
  21.   
  22. if __name__ == "__main__"
  23.     clear_mem(sys.argv[1]) 
  24.    
  25. $ cat mall_mem.txt  
  26. $IP1 $PORT1 
  27. $IP1 $PORT2 
  28. ...  

3. 远程快速校验多台主机不同目录下的文件内容一致性(扩容时发挥作用很大)

  1. $ ssh -t $ip 'find $dir1 $dir2 ... -type f -exec md5sum {} \;' > $ip.md5 # 注意$dir1..等使用绝对路径 
  2. $ doc2unix $ip.md5  #(这个很隐秘,多了windows的回车符) 
  3. $ ssh -tt $ip.other 'md5sum -c --quiet' < $ip.md5  
  4. # 如果一致,什么也不反回;不一致的会告诉NOT MATCH的文件  

哎,非逼自己用python去写,实现了find + sha256sum,但想将其保存到特定文件中,这一点却没实现。这一版,只是想回顾“装饰器”,这里没起到作用。

  1. #!/usr/bin/env python 
  2.   
  3. import os, os.path, sys 
  4. import hashlib 
  5. from functools import wraps 
  6.   
  7. def search(fn): 
  8.     @wraps(fn) 
  9.     def wrap(*args, **kwargs): 
  10.         paths = list(args) 
  11.         ret = fn(*args, **kwargs) 
  12.         #paths.pop() 
  13.         for path in paths: 
  14.             print(path) 
  15.             for pathname in os.listdir(path): 
  16.                 pathname = os.path.join(path, pathname) 
  17.                 if os.path.isfile(pathname): 
  18.                     with open(pathname, 'rb'as f: 
  19.                         m = hashlib.sha256(f.read()) 
  20.                         # hashfile = path + '.sha256' 
  21.                         # print(hashfile) 
  22.                         # with open(hashfile, 'a+'as f: 
  23.                         #     f.write('{}  {}\n'.format(m.hexdigest(), pathname)) 
  24.                         print('%s  %s' % (m.hexdigest(), pathname)) 
  25.                         #ret.write('aaa'
  26.                         #ret.write('%s  %s' % (m.hexdigest(), pathname)) 
  27.                         #ret.close() 
  28.                 if os.path.isdir(pathname): 
  29.                     wrap(pathname) 
  30.     return wrap 
  31.   
  32. @search 
  33. def write(*args, **kwargs): 
  34.     pass 
  35.     #hashfile = list(args).pop() 
  36.     #print(hashfile) 
  37.     #hashfile = '10.255.201.10' 
  38.     #f = open(hashfile, 'a+'
  39.     #return f 
  40.   
  41. if __name__ == '__main__'
  42.     write(*sys.argv[1:])  

4. 配置Open-falcon HostGroups时,根据hostname绑定template,快速获取平台的主机名

  1. $ ansible $group1 -m setup -a 'filter=ansible_hostname' -o | awk -F'[ :|"{}]' '{print $17}' 

5. 关闭远程Nginx\Tomcat进程,启动服务就不写了

  1. $ cat marketapi_stop.sh  
  2. #!/bin/bash 
  3. kill -QUIT $(cat /home/aspire/config/nginx/nginx.pid) 
  4. $ ansible $group1 -m script -a 'marketapi_stop.sh' -s 
  5. ------- 
  6. $ cat tomcat_stop.sh  
  7. #!/bin/bash 
  8. kill -9 $(ps aux | awk '/tomat808[0]\/conf/{print $2}'
  9. $ ansible $group1 -m script -a 'tomcat_stop.sh' -s  

6. 远程tail -f 查看日志滚动

  1. $ ssh -t $IP 'tail -f xxxx.log' 
责任编辑:庞桂玉 来源: 51CTO博客
相关推荐

2012-02-29 00:57:41

Linux系统

2011-04-02 10:13:36

Linux系统管理

2010-03-18 16:57:02

Linux命令

2010-03-18 16:51:32

2010-03-18 16:48:22

Linux命令

2009-10-22 13:23:34

linux系统管理

2012-11-01 11:33:11

IBMdw

2010-12-28 09:16:00

2009-10-12 11:14:51

LinuxLinux磁盘文件系统管理

2010-03-04 14:44:05

Linux管理命令

2011-09-29 09:41:24

系统管理项目管理系统

2010-05-05 15:56:37

Unix系统

2010-02-24 09:13:04

2012-09-24 10:14:46

Linux系统管理

2023-08-28 10:49:13

Linux系统

2011-09-01 13:42:15

优化布线系统管理布线系统

2010-05-05 16:27:22

Unix系统

2010-05-04 15:22:25

Unix系统

2013-04-17 14:37:39

Linux系统管理员susudo

2010-05-07 16:35:44

点赞
收藏

51CTO技术栈公众号