centos find cmin(centos7卸载软件命令)

各位老铁们好,相信很多人对centos find cmin都不是特别的了解,因此呢,今天就来为大家分享下关于centos find cmin以及centos7卸载软件命令的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

python 多进程和多线程配合

由于python的多线程中存在PIL锁,因此python的多线程不能利用多核,那么,由于现在的计算机是多核的,就不能充分利用计算机的多核资源。但是python中的多进程是可以跑在不同的cpu上的。因此,尝试了多进程+多线程的方式,来做一个任务。比如:从中科大的镜像源中下载多个rpm包。

#!/usr/bin/pythonimport reimport commandsimport timeimport multiprocessingimport threadingdef download_image(url):

print'*****the%s rpm begin to download*******'% url

commands.getoutput('wget%s'% url)def get_rpm_url_list(url):

commands.getoutput('wget%s'% url)

rpm_info_str= open('index.html').read()

regu_mate='(?<=<a href=")(.*?)(?=">)'

rpm_list= re.findall(regu_mate, rpm_info_str)

rpm_url_list= [url+ rpm_name for rpm_name in rpm_list] print'the count of rpm list is:', len(rpm_url_list) return rpm_url_list123456789101112131415161718192021

def multi_thread(rpm_url_list):

threads= []# url=';

# rpm_url_list= get_rpm_url_list(url)

for index in range(len(rpm_url_list)): print'rpm_url is:', rpm_url_list[index]

one_thread= threading.Thread(target=download_image, args=(rpm_url_list[index],))

threads.append(one_thread)

thread_num= 5# set threading pool, you have put 4 threads in it

while 1:

count= min(thread_num, len(threads)) print'**********count*********', count###25,25,...6707%25

res= [] for index in range(count):

x= threads.pop()

res.append(x) for thread_index in res:

thread_index.start() for j in res:

j.join() if not threads: break1234567891011121314151617181920212223242526

def multi_process(rpm_url_list):

# process num at the same time is 4

process= []

rpm_url_group_0= []

rpm_url_group_1= []

rpm_url_group_2= []

rpm_url_group_3= [] for index in range(len(rpm_url_list)): if index% 4== 0:

rpm_url_group_0.append(rpm_url_list[index]) elif index% 4== 1:

rpm_url_group_1.append(rpm_url_list[index]) elif index% 4== 2:

rpm_url_group_2.append(rpm_url_list[index]) elif index% 4== 3:

rpm_url_group_3.append(rpm_url_list[index])

rpm_url_groups= [rpm_url_group_0, rpm_url_group_1, rpm_url_group_2, rpm_url_group_3] for each_rpm_group in rpm_url_groups:

each_process= multiprocessing.Process(target= multi_thread, args=(each_rpm_group,))

process.append(each_process) for one_process in process:

one_process.start() for one_process in process:

one_process.join()# for each_url in rpm_url_list:# print'*****the%s rpm begin to download*******'%each_url## commands.getoutput('wget%s'%each_url)123456789101112131415161718192021222324252627282930313233

def main():

url=';

url_paas=';

url_paas2=';

start_time= time.time()

rpm_list= get_rpm_url_list(url_paas) print multi_process(rpm_list)# print multi_thread(rpm_list)

#print multi_process()

# print multi_thread(rpm_list)

# for index in range(len(rpm_list)):

# print'rpm_url is:', rpm_list[index]

end_time= time.time() print'the download time is:', end_time- start_timeprint main()123456789101112131415161718

代码的功能主要是这样的:

main()方法中调用get_rpm_url_list(base_url)方法,获取要下载的每个rpm包的具体的url地址。其中base_url即中科大基础的镜像源的地址,比如:,这个地址下有几十个rpm包,get_rpm_url_list方法将每个rpm包的url地址拼出来并返回。

multi_process(rpm_url_list)启动多进程方法,在该方法中,会调用多线程方法。该方法启动4个多进程,将上面方法得到的rpm包的url地址进行分组,分成4组,然后每一个组中的rpm包再最后由不同的线程去执行。从而达到了多进程+多线程的配合使用。

代码还有需要改进的地方,比如多进程启动的进程个数和rpm包的url地址分组是硬编码,这个还需要改进,毕竟,不同的机器,适合同时启动的进程个数是不同的。

Centos文件搜索命令的讲解

1、locate--->文件查找事先生成的数据库,模糊查找,updatedb更新locate数据库

-i:忽略文件名的大小写

-n:只显示前n行eg:locate-n3passwd

2、find---->实时查找,精确匹配文件名

find[DIR...][CRIERIA][ACTION...]

DIR:/root/home..(默认当前目录)

ACTION;

-print(默认)

-ls

-okCOMMAND交互式执行命令

-execCOMMAND非交互式执行命令

xargs从标准输出中执行和创建命令

eg;find/root-name"[[:alpha:]][[:digit:]]"-execmv{}{}\;//{}代表前面查找的结果,后面必须用\;结尾

find/root-name"[[:alpha:]][[:digit:]]"|xargschmod755

CRIERIA:

-name:精确到指定的文件名

-iname:根据文件名查找,但不区分大小写

-user:根据属主查找

-group:根据属组查找

-uid:根据uid查找

-gid:根据gid查找

在指定时间段里查找:

-atime:[+/-]N(accesstime):+-表示---(+)----N---(-)----currentday

eg:find-atime-3-execls-l{}\;

-mtime:(modificationtime)同上

-ctime(changetime)同上

-amin以分种为单位,同上

-mmin

-cmin

-anewera.txt比a.txt更近的访问

eg:find-anewersh01.sh-execls-l{}\;

-newer相当于modificationtime

-cnewer相当于changetime

-type:按照类型查找

d(目录),l(链接文件),f(普通文件),s(套接文件),

b(块设备),c(字符设备),p(命令管道文件)

eg:find/var-typel-execls-l{}\;

-size:按照大小查找

[+/-]N

-perm[+/-]mode根据权限查找

mode755

-222每一类用户都要匹配

eg:find/tmp/myscrip-perm-001

/222某一个用户只要有一类权限的即可

-nouser:没有用户

eg:find/-nouser查看没有用户名的文件,一般这种文件有一定的危险性

-nogroup:没有组的

查找条件连接:

-a:&&通常可以省略

eg:find-userroot-typef-execls-dl{}\;

-o:||

eg:find-usernamed-o-typed-execls-ld{}\;

-notor!:!

eg:find-not\(-typed-a-userroot\)-execls-ld{}\;

-ls-l`find/-namepasswd`

文件名通配:*任意一个字符,?单个字符,[]

eg:find/tmp-namea*//查找以a开头的文件

Linux系统的OOM Killer处理机制

最近有位 VPS客户抱怨 MySQL无缘无故挂掉,还有位客户抱怨 VPS经常死机,登陆到终端看了一下,都是常见的 Out of memory问题。这通常是因为某时刻应用程序大量请求内存导致系统内存不足造成的,这通常会触发 Linux内核里的 Out of Memory(OOM) killer,OOM killer会杀掉某个进程以腾出内存留给系统用,不致于让系统立刻崩溃。如果检查相关的日志文件(/var/log/messages)就会看到下面类似的 Out of memory: Kill process信息:

...

Out of memory: Kill process 9682(mysqld) score 9 or sacrifice child

Killed process 9682, UID 27,(mysqld) total-vm:47388kB, anon-rss:3744kB, file-rss:80kB

httpd invoked oom-killer: gfp_mask=0x201da, order=0, oom_adj=0, oom_score_adj=0

httpd cpuset=/ mems_allowed=0

Pid: 8911, comm: httpd Not tainted 2.6.32-279.1.1.el6.i686#1

...

21556 total pagecache pages

21049 pages in swap cache

Swap cache stats: add 12819103, delete 12798054, find 3188096/4634617

Free swap= 0kB

Total swap= 524280kB

131071 pages RAM

0 pages HighMem

3673 pages reserved

67960 pages shared

124940 pages non-shared

Linux内核根据应用程序的要求分配内存,通常来说应用程序分配了内存但是并没有实际全部使用,为了提高性能,这部分没用的内存可以留作它用,这部分内存是属于每个进程的,内核直接回收利用的话比较麻烦,所以内核采用一种过度分配内存(over-commit memory)的办法来间接利用这部分“空闲”的内存,提高整体内存的使用效率。一般来说这样做没有问题,但当大多数应用程序都消耗完自己的内存的时候麻烦就来了,因为这些应用程序的内存需求加起来超出了物理内存(包括 swap)的容量,内核(OOM killer)必须杀掉一些进程才能腾出空间保障系统正常运行。用银行的例子来讲可能更容易懂一些,部分人取钱的时候银行不怕,银行有足够的存款应付,当全国人民(或者绝大多数)都取钱而且每个人都想把自己钱取完的时候银行的麻烦就来了,银行实际上是没有这么多钱给大家取的。

内核检测到系统内存不足、挑选并杀掉某个进程的过程可以参考内核源代码 linux/mm/oom_kill.c,当系统内存不足的时候,out_of_memory()被触发,然后调用 select_bad_process()选择一个“bad”进程杀掉,如何判断和选择一个“bad”进程呢,总不能随机选吧?挑选的过程由 oom_badness()决定,挑选的算法和想法都很简单很朴实:最 bad的那个进程就是那个最占用内存的进程。

/**

* oom_badness- heuristic function to determine which candidate task to kill

*@p: task struct of which task we should calculate

*@totalpages: total present RAM allowed for page allocation

*

* The heuristic for determining which task to kill is made to be as simple and

* predictable as possible. The goal is to return the highest value for the

* task consuming the most memory to avoid subsequent oom failures.

*/

unsigned long oom_badness(struct task_struct*p, struct mem_cgroup*memcg,

const nodemask_t*nodemask, unsigned long totalpages)

{

long points;

long adj;

if(oom_unkillable_task(p, memcg, nodemask))

return 0;

p= find_lock_task_mm(p);

if(!p)

return 0;

adj=(long)p-signal-oom_score_adj;

if(adj== OOM_SCORE_ADJ_MIN){

task_unlock(p);

return 0;

}

/*

* The baseline for the badness score is the proportion of RAM that each

* task's rss, pagetable and swap space use.

*/

points= get_mm_rss(p-mm)+ p-mm-nr_ptes+

get_mm_counter(p-mm, MM_SWAPENTS);

task_unlock(p);

/*

* Root processes get 3% bonus, just like the __vm_enough_memory()

* implementation used by LSMs.

*/

if(has_capability_noaudit(p, CAP_SYS_ADMIN))

adj-= 30;

/* Normalize to oom_score_adj units*/

adj*= totalpages/ 1000;

points+= adj;

/*

* Never return 0 for an eligible task regardless of the root bonus and

* oom_score_adj(oom_score_adj can't be OOM_SCORE_ADJ_MIN here).

*/

return points 0? points: 1;

}

上面代码里的注释写的很明白,理解了这个算法我们就理解了为啥 MySQL躺着也能中枪了,因为它的体积总是最大(一般来说它在系统上占用内存最多),所以如果 Out of Memeory(OOM)的话总是不幸第一个被 kill掉。解决这个问题最简单的办法就是增加内存,或者想办法优化 MySQL使其占用更少的内存,除了优化 MySQL外还可以优化系统(优化 Debian 5,优化 CentOS 5.x),让系统尽可能使用少的内存以便应用程序(如 MySQL)能使用更多的内存,还有一个临时的办法就是调整内核参数,让 MySQL进程不容易被 OOM killer发现。

我们可以通过一些内核参数来调整 OOM killer的行为,避免系统在那里不停的杀进程。比如我们可以在触发 OOM后立刻触发 kernel panic,kernel panic 10秒后自动重启系统。

# sysctl-w vm.panic_on_oom=1

vm.panic_on_oom= 1

# sysctl-w kernel.panic=10

kernel.panic= 10

# echo"vm.panic_on_oom=1"/etc/sysctl.conf

# echo"kernel.panic=10"/etc/sysctl.conf

从上面的 oom_kill.c代码里可以看到 oom_badness()给每个进程打分,根据 points的高低来决定杀哪个进程,这个 points可以根据 adj调节,root权限的进程通常被认为很重要,不应该被轻易杀掉,所以打分的时候可以得到 3%的优惠(adj-= 30;分数越低越不容易被杀掉)。我们可以在用户空间通过操作每个进程的 oom_adj内核参数来决定哪些进程不这么容易被 OOM killer选中杀掉。比如,如果不想 MySQL进程被轻易杀掉的话可以找到 MySQL运行的进程号后,调整 oom_score_adj为-15(注意 points越小越不容易被杀):

# ps aux| grep mysqld

mysql 2196 1.6 2.1 623800 44876? Ssl 09:42 0:00/usr/sbin/mysqld

# cat/proc/2196/oom_score_adj

0

# echo-15/proc/2196/oom_score_adj

当然,如果需要的话可以完全关闭 OOM killer(不推荐用在生产环境):

# sysctl-w vm.overcommit_memory=2

# echo"vm.overcommit_memory=2"/etc/sysctl.conf

我们知道了在用户空间可以通过操作每个进程的 oom_adj内核参数来调整进程的分数,这个分数也可以通过 oom_score这个内核参数看到,比如查看进程号为981的 omm_score,这个分数被上面提到的 omm_score_adj参数调整后(-15),就变成了3:

# cat/proc/981/oom_score

18

# echo-15/proc/981/oom_score_adj

# cat/proc/981/oom_score

3

下面这个 bash脚本可用来打印当前系统上 oom_score分数最高(最容易被 OOM Killer杀掉)的进程:

# vi oomscore.sh

#!/bin/bash

for proc in$(find/proc-maxdepth 1-regex'/proc/[0-9]+'); do

printf"%2d%5d%sn"

"$(cat$proc/oom_score)"

"$(basename$proc)"

"$(cat$proc/cmdline| tr''''| head-c 50)"

done 2/dev/null| sort-nr| head-n 10

# chmod+x oomscore.sh

#./oomscore.sh

18 981/usr/sbin/mysqld

4 31359-bash

4 31056-bash

1 31358 sshd: root@pts/6

1 31244 sshd: vpsee [priv]

1 31159-bash

1 31158 sudo-i

1 31055 sshd: root@pts/3

1 30912 sshd: vpsee [priv]

1 29547/usr/sbin/sshd-D

阅读剩余
THE END