Elton's Blog

Tag: Linux

Nginx使用Linux内存加速静态文件访问

by on 二.05, 2012, under Linux, Web

Nginx是一个非常出色的静态资源web服务器。如果你嫌它还不够快,可以把放在磁盘中的文件,映射到内存中,减少高并发下的磁盘IO。

先做几个假设。nginx.conf中所配置站点的路径是/home/wwwroot/res,站点所对应文件原始存储路径:/opt/web/res

shell脚本非常简单,思路就是拷贝资源文件到内存中,然后在把网站的静态文件链接指向到内存中即可。具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#! /bin/bash   
 
res_path="/opt/web/res"  
mem_path="/dev/shm/res"  
lk_path="/home/wwwroot/res"  
 
if [ ! -d "$mem_path" ]; then  
        cp -r "$res_path" "$mem_path"  
fi  
 
if [ ! -L "$lk_path" ]; then  
        ln -s "$mem_path" "$lk_path"  
fi
Leave a Comment :, , more...

MongoDB与内存管理

by on 八.20, 2011, under Database, Linux

但凡初次接触MongoDB的人,无不惊讶于它对内存的贪得无厌,至于个中缘由,我先讲讲Linux是如何管理内存的,再说说MongoDB是如何使用内存的,答案自然就清楚了。

据说带着问题学习更有效,那就先看一个MongoDB服务器的top命令结果:

1
2
3
4
5
6
shell> top -p $(pidof mongod)
Mem:  32872124k total, 30065320k used,  2806804k free,   245020k buffers
Swap:  2097144k total,      100k used,  2097044k free, 26482048k cached
 
 VIRT  RES  SHR %MEM
1892g  21g  21g 69.6

这台MongoDB服务器有没有性能问题?大家可以一边思考一边继续阅读。

先讲讲Linux是如何管理内存的

在Linux里(别的系统也差不多),内存有物理内存和虚拟内存之说,物理内存是什么自然无需解释,虚拟内存实际是物理内存的抽象,多数情况下,出于方便性的考虑,程序访问的都是虚拟内存地址,然后操作系统会把它翻译成物理内存地址。

很多人会把虚拟内存和Swap混为一谈,实际上Swap只是虚拟内存引申出的一种技术而已:操作系统一旦物理内存不足,为了腾出内存空间存放新内容,就会把当前物理内存中的内容放到交换分区里,稍后用到的时候再取回来,需要注意的是,Swap的使用可能会带来性能问题,偶尔为之无需紧张,糟糕的是物理内存和交换分区频繁的发生数据交换,这被称之为Swap颠簸,一旦发生这种情况,先要明确是什么原因造成的,如果是内存不足就好办了,加内存就可以解决,不过有的时候即使内存充足也可能会出现这种问题,比如MySQL就有可能出现这样的情况,解决方法是限制使用Swap:

1
shell> sysctl -w vm.swappiness=0

查看内存情况最常用的是free命令:

1
2
3
4
5
shell> free -m
             total       used       free     shared    buffers     cached
Mem:         32101      29377       2723          0        239      25880
-/+ buffers/cache:       3258      28842
Swap:         2047          0       2047

新手看到used一栏数值偏大,free一栏数值偏小,往往会认为内存要用光了。其实并非如此,之所以这样是因为每当我们操作文件的时候,Linux都会尽可能的把文件缓存到内存里,这样下次访问的时候,就可以直接从内存中取结果,所以cached一栏的数值非常的大,不过不用担心,这部分内存是可回收的,操作系统会按照LRU算法淘汰冷数据。还有一个buffers,也是可回收的,它和cache的区别,可以参考维基百科

知道了原理,我们就可以推算出系统可用的内存是free + buffers + cached:

1
2
shell> echo "2723 + 239 + 25880" | bc -l
28842

至于系统实际使用的内存是used – buffers – cached:

1
2
shell> echo "29377 - 239 - 25880" | bc -l
3258

除了free命令,还可以使用sar命令:

1
2
3
4
5
6
7
shell> sar -r
kbmemfree kbmemused  %memused kbbuffers  kbcached
  3224392  29647732     90.19    246116  26070160
 
shell> sar -W
pswpin/s pswpout/s
    0.00      0.00

希望你没有被%memused吓到,如果不幸言中,重读本文。

再说说MongoDB是如何使用内存的

目前,MongoDB使用的是内存映射存储引擎,它会把磁盘IO操作转换成内存操作,如果是读操作,内存中的数据起到缓存的作用,如果是写操作,内存还可以把随机的写操作转换成顺序的写操作,总之可以大幅度提升性能。MongoDB并不干涉内存管理工作,而是把这些工作留给操作系统的虚拟内存管理器去处理,这样的好处是简化了MongoDB的工作,但坏处是你没有方法很方便的控制MongoDB占多大内存,事实上MongoDB会占用所有能用的内存,所以最好不要把别的服务和MongoDB放一起。

有时候,即便MongoDB使用的是64位操作系统,也可能会遭遇臭名昭著的OOM问题,出现这种情况,多半是因为限制了虚拟内存的大小所致,可以这样查看当前值:

1
shell> ulimit -a | grep 'virtual'

多数操作系统缺省都是把它设置成unlimited的,如果你的操作系统不是,可以这样修改:

1
shell> ulimit -v unlimited

不过要注意的是,ulimit的使用是有上下文的,最好放在MongoDB的启动脚本里。

有时候,出于某些原因,你可能想释放掉MongoDB占用的内存,不过前面说了,内存管理工作是由虚拟内存管理器控制的,所以通常你只能通过重启服务来释放内存,你一定不齿于这样的方法,幸好可以使用MongoDB内置的closeAllDatabases命令达到目的:

1
2
mongo> use admin
mongo> db.runCommand({closeAllDatabases:1})

另外,通过调整内核参数drop_caches也可以释放缓存:

1
shell> sysctl -w vm.drop_caches=1

平时可以通过mongo命令行来监控MongoDB的内存使用情况,如下所示:

1
2
3
4
5
6
mongo> db.serverStatus().mem:
{
    "resident" : 22346,
    "virtual" : 1938524,
    "mapped" : 962283
}

还可以通过mongostat命令来监控MongoDB的内存使用情况,如下所示:

1
2
3
shell> mongostat
mapped  vsize    res faults
  940g  1893g  21.9g      0

其中内存相关字段的含义是:

  • mapped:映射到内存的数据大小
  • visze:占用的虚拟内存大小
  • res:占用的驻留内存大小

注:如果操作不能在内存中完成,结果faults列的数值不会是0,视大小可能有性能问题。

在上面的结果中,vsize是mapped的两倍,而mapped等于数据文件的大小,所以说vsize是数据文件的两倍,之所以会这样,是因为本例中,MongoDB开启了journal,需要在内存里多映射一次数据文件,如果关闭journal,则vsize和mapped大致相当。

如果想验证这一点,可以在开启或关闭journal后,通过pmap命令来观察文件映射情况:

1
shell> pmap $(pidof mongod)

到底MongoDB配备多大内存合适?宽泛点来说,多多益善,如果要确切点来说,这实际取决于你的数据及索引的大小,内存如果能够装下全部数据加索引是最佳情况,不过很多时候,数据都会比内存大,比如本文所涉及的MongoDB实例:

1
2
3
4
5
mongo> db.stats()
{
    "dataSize" : 1004862191980,
    "indexSize" : 1335929664
}

本例中索引只有1G多,内存完全能装下,而数据文件则达到了1T,估计很难找到这么大内存,此时保证内存能装下热数据即可,至于热数据是多少,取决于具体的应用。如此一来内存大小就明确了:内存 > 索引 + 热数据。

原文引自:火丁笔记

Leave a Comment :, more...

安装史上最牛的vimrc

by on 七.19, 2011, under Linux

平时大家都会用到vim,发现一个不错的vimrc,大家可以试着用下。

通过一下命令安装这个vimrc

1
2
3
4
5
$ mkdir ~/.vim_runtime
$ svn co svn://orangoo.com/vim ~/.vim_runtime
$ cat ~/.vim_runtime/install.sh
$ sh ~/.vim_runtime/install.sh <system>
  <sytem> can be `mac`, `linux` or `windows`

安装好后的vim看起来像这个样子

4 Comments :, more...

ubuntu下自动安装雅黑字体脚本

by on 七.18, 2011, under Linux

安装ubuntu的时候,总是需要美化中文字体。 微软雅黑是比较好的中文字体美化方案。 下面的这个脚本可以帮助大家自动来美化中文字体。

1
2
3
4
wget -O get-fonts.sh.zip http://files.cnblogs.com/DengYangjun/get-fonts.sh.zip
unzip -o get-fonts.sh.zip 1>/dev/null
chmod a+x get-fonts.sh
./get-fonts.sh

删除下载的字体安装脚本文件:

1
rm get-fonts.sh get-fonts.sh.zip 2>/dev/null

恢复以前的字体设置:

1
2
3
4
5
cd /etc/fonts/conf.avail
sudo mv 51-local.conf.old 51-local.conf 2>/dev/null
sudo mv 69-language-selector-zh-cn.conf.old 69-language-selector-zh-cn.conf 2>/dev/null
sudo rm -f -r /usr/share/fonts/truetype/myfonts 2>/dev/null
cd -

修正记录:
#1.添加了最新的Windows 7的微软雅黑字体。(附件大小限制,未实现)
2.修正了CRT渲染的配置文件的链接错误。
3.添加字体:Agency FB
4.添加字体设置恢复功能。

2 Comments :, , more...

ubuntu 11.04下,使用google-perftools的tcmalloc加速MySQL

by on 七.15, 2011, under Linux

1.在 /usr/local/src 下准备好以下文件
libunwind-0.99.tar.gz (64位操作系统需要)
google-perftools-1.7.tar.gz

2.安装libunwind(32位操作系统忽略此步骤)

1
2
3
4
5
6
cd /usr/local/src
tar zxvf libunwind-0.99.tar.gz
cd libunwind-0.99
CFLAGS=-fPIC ./configure --enable-shared
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

3.安装google-perftools (最小化安装)

1
2
3
4
5
6
cd /usr/local/src
tar zvxf google-perftools-1.7.tar.gz
cd google-perftools-1.7
mkdir /tmp/tc
./configure --prefix=/tmp/tc --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal
 make && make install

4.复制tcmalloc动态库到系统库目录,并建立软连接

1
2
3
4
5
6
ls -alt /tmp/tc/lib/*
cp /tmp/tc/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so.0
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so.0.1.0
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc_minimal.so.0

5.删除临时目录

1
rm -rf /tmp/tc

6.设置Mysql运行时加载tcmalloc (根据mysql安装位置而定)

1
vim /usr/local/mysql/bin/mysqld_safe

在# executing mysqld_safe的下一行,加入以下内容

1
export LD_PRELOAD="/usr/local/lib/libtcmalloc.so"

7. 重启MySQL

1
/etc/init.d/mysql start

8.使用lsof命令查看tcmalloc是否起效

1
sudo lsof -n |grep tcmalloc

应该看到下面的内容

1
mysqld    12484      mysql  mem       REG        8,7       860657    5382577 /usr/local/lib/libtcmalloc_minimal.so.0.1.0

说明MySQL已经使用上了tcmalloc进行加速了。

1 Comment :, , more...

ubuntu 11.04国内镜像源

by on 七.15, 2011, under Linux

Ubuntu 11.04网易源(速度很快,推荐)

deb http://mirrors.163.com/ubuntu/ natty main universe restricted multiverse
deb-src http://mirrors.163.com/ubuntu/ natty main universe restricted multiverse
deb http://mirrors.163.com/ubuntu/ natty-security universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ natty-security universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ natty-updates universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ natty-proposed universe main multiverse restricted
deb http://mirrors.163.com/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirrors.163.com/ubuntu/ natty-updates universe main multiverse restricted

lupaworld 源,提供 ArchLinux,Everest,FreeBSD,fedora,LFS,Magiclinux,Mandriva,OpenOffice,openSUSE,Puppy,Red Hat,Slitaz,Turbolinux,Ubuntu,中标普华

deb http://mirror.lupaworld.com/ubuntu/ natty main universe restricted multiverse
deb-src http://mirror.lupaworld.com/ubuntu/ natty main universe restricted multiverse
deb http://mirror.lupaworld.com/ubuntu/ natty-security universe main multiverse restricted
deb-src http://mirror.lupaworld.com/ubuntu/ natty-security universe main multiverse restricted
deb http://mirror.lupaworld.com/ubuntu/ natty-updates universe main multiverse restricted
deb http://mirror.lupaworld.com/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://mirror.lupaworld.com/ubuntu/ natty-proposed universe main multiverse restricted
deb http://mirror.lupaworld.com/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.lupaworld.com/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.lupaworld.com/ubuntu/ natty-updates universe main multiverse restricted

ubuntu 11.04 搜狐源

deb http://mirrors.shlug.org/ubuntu/ natty main universe restricted multiverse
deb-src http://mirrors.shlug.org/ubuntu/ natty main universe restricted multiverse
deb http://mirrors.shlug.org/ubuntu/ natty-security universe main multiverse restricted
deb-src http://mirrors.shlug.org/ubuntu/ natty-security universe main multiverse restricted
deb http://mirrors.shlug.org/ubuntu/ natty-updates universe main multiverse restricted
deb http://mirrors.shlug.org/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://mirrors.shlug.org/ubuntu/ natty-proposed universe main multiverse restricted
deb http://mirrors.shlug.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirrors.shlug.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirrors.shlug.org/ubuntu/ natty-updates universe main multiverse restricted

deb http://cn.archive.ubuntu.com/ubuntu/ natty multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ natty multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ natty-updates multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ natty-updates multiverse
deb http://security.ubuntu.com/ubuntu natty-security main restricted
deb-src http://security.ubuntu.com/ubuntu natty-security main restricted
deb http://security.ubuntu.com/ubuntu natty-security universe
deb-src http://security.ubuntu.com/ubuntu natty-security universe
deb http://security.ubuntu.com/ubuntu natty-security multiverse
deb-src http://security.ubuntu.com/ubuntu natty-security multiverse
deb http://archive.canonical.com/ubuntu natty partner
deb-src http://archive.canonical.com/ubuntu natty partner

ubuntu 11.04台湾源

deb http://tw.archive.ubuntu.com/ubuntu/ natty main universe restricted multiverse
deb-src http://tw.archive.ubuntu.com/ubuntu/ natty main universe restricted multiverse
deb http://tw.archive.ubuntu.com/ubuntu/ natty-security universe main multiverse restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ natty-security universe main multiverse restricted
deb http://tw.archive.ubuntu.com/ubuntu/ natty-updates universe main multiverse restricted
deb-src http://tw.archive.ubuntu.com/ubuntu/ natty-updates universe main multiverse restricted

SRT Sources

deb http://ubuntu.srt.cn/ubuntu/ natty main restricted universe multiverse
deb http://ubuntu.srt.cn/ubuntu/ natty-security main restricted universe multiverse
deb http://ubuntu.srt.cn/ubuntu/ natty-updates main restricted universe multiverse
deb http://ubuntu.srt.cn/ubuntu/ natty-proposed main restricted universe multiverse
deb http://ubuntu.srt.cn/ubuntu/ natty-backports main restricted universe multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty main restricted universe multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty-security main restricted universe multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty-updates main restricted universe multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty-proposed main restricted universe multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty-backports main restricted universe multiverse

ubuntu 11.04 上海源 提供 Kernel,Hiweed,ubuntu

deb http://mirror.rootguide.org/ubuntu/ natty main universe restricted multiverse
deb-src http://mirror.rootguide.org/ubuntu/ natty main universe restricted multiverse
deb http://mirror.rootguide.org/ubuntu/ natty-security universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-security universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-updates universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-proposed universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-updates universe main multiverse restricted

ubuntu 11.04 骨头源 骨头源是bones7456架设的一个Ubuntu源 ,提供ubuntu,deepin

deb http://ubuntu.srt.cn/ubuntu/ natty main universe restricted multiverse
deb-src http://ubuntu.srt.cn/ubuntu/ natty main universe restricted multiverse
deb http://ubuntu.srt.cn/ubuntu/ natty-security universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ natty-security universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ natty-updates universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ natty-proposed universe main multiverse restricted
deb http://ubuntu.srt.cn/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://ubuntu.srt.cn/ubuntu/ natty-updates universe main multiverse restricted

rootguide源,ubuntu官方上海源,提供 Kernel,Hiweed,ubuntu

deb http://mirror.rootguide.org/ubuntu/ natty main universe restricted multiverse
deb-src http://mirror.rootguide.org/ubuntu/ natty main universe restricted multiverse
deb http://mirror.rootguide.org/ubuntu/ natty-security universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-security universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-updates universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-proposed universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-proposed universe main multiverse restricted
deb http://mirror.rootguide.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-backports universe main multiverse restricted
deb-src http://mirror.rootguide.org/ubuntu/ natty-updates universe main multiverse restricted

PS: 建议保留官方的那个security 的源 其他的官方源就可以删掉

Leave a Comment :, , more...

struct stat

by on 十二.03, 2009, under Linux

Unix like的开发如果用stat()函数访问文件的话,会用到struct stat结构体。 其定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct stat {
        mode_t     st_mode;       //文件对应的模式,文件,目录等
        ino_t      st_ino;       //inode节点号
        dev_t      st_dev;        //设备号码
        dev_t      st_rdev;       //特殊设备号码
        nlink_t    st_nlink;      //文件的连接数
        uid_t      st_uid;        //文件所有者
        gid_t      st_gid;        //文件所有者对应的组
        off_t      st_size;       //普通文件,对应的文件字节数
        time_t     st_atime;      //文件最后被访问的时间
        time_t     st_mtime;      //文件内容最后被修改的时间
        time_t     st_ctime;      //文件状态改变时间
        blksize_t st_blksize;    //文件内容对应的块大小
        blkcnt_t   st_blocks;     //伟建内容对应的块数量
      };
Leave a Comment :, , more...

Linux文件查找命令find,xargs详述

by on 十二.01, 2009, under Linux

前言:关于find命令

由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。

在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

一、find 命令格式

1、find命令的一般形式为;

1
find pathname -options [-print -exec -ok ...]

2、find命令的参数;

1
2
3
4
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {  } ;,注意{   }和;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

3、find命令选项

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
-name 
按照文件名查找文件。
 
-perm 
按照文件权限来查找文件。
 
-prune 
使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
 
-user 
按照文件属主来查找文件。
 
-group 
按照文件所属的组来查找文件。
 
-mtime -n +n 
按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
 
-nogroup 
查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
 
-nouser 
查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 
 
查找更改时间比文件file1新但比文件file2旧的文件。
-type 
 
查找某一类型的文件,诸如:
 
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
 
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
 
-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。

另外,下面三个的区别:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   -amin n
  查找系统中最后N分钟访问的文件
 
  -atime n
  查找系统中最后n*24小时访问的文件
 
  -cmin n
  查找系统中最后N分钟被改变文件状态的文件
 
  -ctime n
  查找系统中最后n*24小时被改变文件状态的文件
 
    -mmin n
  查找系统中最后N分钟被改变文件数据的文件
 
  -mtime n
  查找系统中最后n*24小时被改变文件数据的文件

4、使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的

在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

例如:为了用ls -l命令列出所匹配到的文件,可以把ls -l命令放在find命令的-exec选项中

1
2
3
4
# find . -type f -exec ls -l {  } ;
-rw-r--r--    1 root     root        34928 2003-02-25  ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25  ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25  ./conf.d/README

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
在/logs目录中查找更改时间在5日以前的文件并删除它们:

1
$ find logs -type f -mtime +5 -exec rm {  } ;

记住:在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。

1
2
$ find . -name "*.conf"  -mtime +5 -ok rm {  } ;
< rm ... ./conf/httpd.conf > ? n

按y键删除文件,按n键不删除。

任何形式的命令都可以在-exec选项中使用。

在下面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个sam用户。

1
2
# find /etc -name "passwd*" -exec grep "sam" {  } ;
sam:x:501:501::/usr/sam:/bin/bash

二、find命令的例子;

1、查找当前用户主目录下的所有文件:

下面两种方法都可以使用

1
2
$ find $HOME -print
$ find ~ -print

2、让当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件;

1
$ find . -type f -perm 644 -exec ls -l {  } ;

3、为了查找系统中所有文件长度为0的普通文件,并列出它们的完整路径;

1
$ find / -type f -size 0 -exec ls -l {  } ;

4、查找/var/logs目录中更改时间在7日以前的普通文件,并在删除之前询问它们;

1
$ find /var/logs -type f -mtime +7 -ok rm {  } ;

5、为了查找系统中所有属于root组的文件;

1
2
$find . -group root -exec ls -l {  } ;
-rw-r--r--    1 root     root          595 1031 01:09 ./fie1

6、find命令将删除当目录中访问时间在7日以来、含有数字后缀的admin.log文件。

该命令只检查三位数字,所以相应文件的后缀不要超过999。先建几个admin.log*的文件 ,才能使用下面这个命令

1
2
3
4
5
6
$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7  -ok
rm {  } ;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n

7、为了查找当前文件系统中的所有目录并排序;

1
$ find . -type d | sort

8、为了查找系统中所有的rmt磁带设备;

1
$ find /dev/rmt -print

三、xargs

1
xargs - build and execute command lines from standard input

在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

来看看xargs命令是如何同find命令一起使用的,并给出一些例子。

下面的例子查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

1
2
3
4
#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text
......

在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

1
$ find / -name "core" -print | xargs echo "" >/tmp/core.log

上面这个执行太慢,我改成在当前目录下查找

1
2
3
#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:

1
2
3
4
5
6
7
8
9
10
# ls -l
drwxrwxrwx    2 sam      adm          4096 1030 20:14 file6
-rwxrwxrwx    2 sam      adm             0 1031 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 1031 01:01 httpd.conf
 
# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 1030 20:14 file6
-rwxrwxr-x    2 sam      adm             0 1031 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 1031 01:01 httpd.conf

用grep命令在所有的普通文件中搜索hostname这个词:

1
2
3
4
# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

1
2
3
4
# find . -name * -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

注意,在上面的例子中, 用来取消find命令中的*在shell中的特殊含义。

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。

四、find 命令的参数

下面是find一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find的命令手册

1、使用name选项

文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。

可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。

不管当前路径是什么,如果想要在自己的根目录$HOME中查找文件名符合*.txt的文件,使用~作为 ‘pathname’参数,波浪号~代表了你的$HOME目录。

1
$ find ~ -name "*.txt" -print

想要在当前目录及子目录中查找所有的‘ *.txt’文件,可以用:

1
$ find . -name "*.txt" -print

想要的当前目录及子目录中查找文件名以一个大写字母开头的文件,可以用:

1
$ find . -name "[A-Z]*" -print

想要在/etc目录中查找文件名以host开头的文件,可以用:

1
$ find /etc -name "host*" -print

想要查找$HOME目录中的文件,可以用:

1
$ find ~ -name "*" -printfind . -print

要想让系统高负荷运行,就从根目录开始查找所有的文件。

1
$ find / -name "*" -print

如果想在当前目录查找文件名以两个小写字母开头,跟着是两个数字,最后是.txt的文件,下面的命令就能够返回名为ax37.txt的文件:

1
$find . -name "[a-z][a-z][0--9][0--9].txt" -print

2、用perm选项

按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。

如在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用:

1
$ find . -perm 755 -print

还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-006相当于666

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ls -l
-rwxrwxr-x    2 sam      adm             0 1031 01:01 http3.conf
-rw-rw-rw-    1 sam      adm         34890 1031 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 1031 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 1026 19:48 sam
-rw-rw-rw-    1 root     root         2792 1031 20:19 temp
 
# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp
-perm mode:文件许可正好符合mode
 
-perm +mode:文件许可部分符合mode
 
-perm -mode: 文件许可完全符合mode

3、忽略某个目录

如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。

如果希望在/apps目录下查找文件,但不希望在/apps/bin目录下查找,可以用:

1
$ find /apps -path "/apps/bin" -prune -o -print

4、使用find查找文件的时候怎么避开某个文件目录

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

1
find /usr/sam -path "/usr/sam/dir1" -prune -o -print

find [-path ..] [expression] 在路径列表的后面的是表达式
-path “/usr/sam” -prune -o -print 是 -path “/usr/sam” -a -prune -o
-print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path “/usr/sam” 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path “/usr/sam” -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例可以用伪码写为

1
2
3
4
if -path "/usr/sam"  then
          -prune
else
          -print

避开多个文件夹

1
find /usr/sam ( -path /usr/sam/dir1 -o -path /usr/sam/file1 ) -prune -o -print

圆括号表示表达式的结合。
表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。
查找某一确定文件,-name等选项加在-o 之后

1
#find /usr/sam  (-path /usr/sam/dir1 -o -path /usr/sam/file1 ) -prune -o -name "temp" -print

5、使用user和nouser选项

按文件属主查找文件,如在$HOME目录中查找文件属主为sam的文件,可以用:

1
$ find ~ -user sam -print

在/etc目录下查找文件属主为uucp的文件:

1
$ find /etc -user uucp -print

为了查找属主帐户已经被删除的文件,可以使用-nouser选项。这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。

例如,希望在/home目录下查找所有的这类文件,可以用:

1
$ find /home -nouser -print

6、使用group和nogroup选项

就像user和nouser选项一样,针对文件所属于的用户组, find命令也具有同样的选项,为了在/apps目录下查找属于gem用户组的文件,可以用:

1
$ find /apps -group gem -print

要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件

1
$ find / -nogroup-print

7、按照更改时间或访问时间等查找文件

如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。

用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。

希望在系统根目录下查找更改时间在5日以内的文件,可以用:

1
$ find / -mtime -5 -print

为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:

1
$ find /var/adm -mtime +3 -print

8、查找比某个文件新或旧的文件

如果希望查找更改时间比某个文件新但比另一个文件旧的所有文件,可以使用-newer选项。它的一般形式为:

newest_file_name ! oldest_file_name
其中,!是逻辑非符号。

查找更改时间比文件sam新但比文件temp旧的文件:

例:有两个文件

1
2
3
4
5
6
7
8
9
10
-rw-r--r--    1 sam      adm             0 1031 01:07 fiel
-rw-rw-rw-    1 sam      adm         34890 1031 00:57 httpd1.conf
-rwxrwxr-x    2 sam      adm             0 1031 01:01 httpd.conf
drw-rw-rw-    2 gem      group        4096 1026 19:48 sam
-rw-rw-rw-    1 root     root         2792 1031 20:19 temp
 
# find -newer httpd1.conf  ! -newer temp -ls
1077669    0 -rwxrwxr-x   2 sam      adm             0 1031 01:01 ./httpd.conf
1077671    4 -rw-rw-rw-   1 root     root         2792 1031 20:19 ./temp
1077673    0 -rw-r--r--   1 sam      adm             0 1031 01:07 ./fiel

查找更改时间在比temp文件新的文件:

1
$ find . -newer temp -print

9、使用type选项
在/etc目录下查找所有的目录,可以用:

1
$ find /etc -type d -print

在当前目录下查找除目录以外的所有类型的文件,可以用:

1
$ find . ! -type d -print

在/etc目录下查找所有的符号链接文件,可以用

1
$ find /etc -type l -print

10、使用size选项

可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。

在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
在当前目录下查找文件长度大于1 M字节的文件:

1
$ find . -size +1000000c -print

在/home/apache目录下查找文件长度恰好为100字节的文件:

1
$ find /home/apache -size 100c -print

在当前目录下查找长度超过10块的文件(一块等于512字节):

1
$ find . -size +10 -print

11、使用depth选项

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

在下面的例子中, find命令从文件系统的根目录开始,查找一个名为CON.FILE的文件。

它将首先匹配所有的文件然后再进入子目录中查找。

1
$ find / -name "CON.FILE" -depth -print

12、使用mount选项

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

从当前目录开始查找位于本文件系统中文件名以XC结尾的文件:

1
$ find . -name "*.XC" -mount -print

五、关于本文

本文是find 命令的详细说明,可贵的是针对参数举了很多的实例,大量的例证,让初学者更为容易理解;本文是zhy2111314兄贴在论坛中;我对本文进行了再次整理,为方便大家阅读; ── 北南南北

转载自:http://www.linuxsir.org/main/?q=node/137

Leave a Comment :, , more...

CentOS 5.3 OpenVZ安装指南

by on 十.27, 2009, under Linux

1. 安装宿主系统CentOS 5.3,分区的时候,要分一个/vz的ext3分区用来存放OpenVZ的template和Virtual Private Servers。 官方的建议是:

/ 2-4G
/swap 2倍物理内存
/vz 剩余的磁盘空间

2. 关掉FIrewall和SELinux
3. 安装OpenVZ
a) 从http://wiki.openvz.org/Download/kernel下载你操作系统对应的内核版本。 我这里用的是ovzkernel-2.6.18-128.2.1.el5.028stab064.7.i686.rpm

1
rpm -ihv ovzkernel-2.6.18-128.2.1.el5.028stab064.7.i686.rpm

b) 编辑GRUB Loader
/boot/grub/grub.conf
确保grub.conf文件的内容为:

1
2
3
4
title OpenVZ (2.6.18-128.2.1.el5.028stab064.7)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.18-128.2.1.el5.028stab064.7 ro root=LABEL=/
        initrd /boot/initrd-2.6.18-128.2.1.el5.028stab064.7.img

c) 设置sysctl参数
/etc/sysctl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
# On Hardware Node we generally need 
# packet forwarding enabled and proxy arp disabled 
net.ipv4.ip_forward = 1 
net.ipv4.conf.default.proxy_arp = 0 
# Enables source route verification 
net.ipv4.conf.all.rp_filter = 1 
# Enables the magic-sysrq key 
kernel.sysrq = 1 
# TCP Explict Congestion Notification
#net.ipv4.tcp_ecn = 0 
# we do not want all our interfaces to send redirects 
net.ipv4.conf.default.send_redirects = 1 
net.ipv4.conf.all.send_redirects = 0

使用下面的命令,使设置生效,之后重启系统。

1
# sysctl -p

c) 安装客户端工具

  • vzctl: 这个工具是用来操作VPS的,如创建,销毁,开始,关闭和设置参数
  • vzquota: 用于设定VPS的 quota
  • vzpkg:这个工具用来管理 OpenVZ的 templates.
1
2
3
4
rpm -Uhv vzyum-2.4.0-11.noarch.rpm
rpm -Uhv vzquota-3.0.12-1.i386.rpm 
rpm -Uhv vzctl-3.0.23-1.i386.rpm 
rpm -Uhv vzpkg-2.7.0-18.noarch.rpm

然后你就可以启动OpenVZ了

1
/etc/init.d/vz start

3.安装OpenVZ template
在这里下载你所需要的模板http://openvz.org/download/template/
先安装模板metadata,再使用vzpkgcache生成cache

或者直接在http://openvz.org/download/template/cache/下载已经cache过的模板,比如centos-5-x86_64.tar.gz ,不用解压,直接把它放到/vz/template/cache中。 然后使用下面的命令来生成虚机

1
vzctl create 101 --ostemplate centos-5-x86 --config vps.basic

create后面的数字是这个VPS的ID,每个VPS都要有一个唯一的ID来做标示。 可以使用ip的最后一位来做标示, 这样方便记忆。

VPS创建后,会在/vz/root/vpsid/生成一个目录作为它的私有空间.
为了便于设置,不必每个VPS都指定参数,创建的时候跟上了一个–config参数用于指定VPS的设置参数。 这些配置文件在/etc/sysconfig/vz-script中。 上面使用的就是/etc/sysconfig/vz-scripts/ve-vps.basic.conf-sample这个文件

你可以通过编辑/etc/sysconfig/vz文件的内容,来预先指定模板和配置文件,如:

1
2
DEF_OSTEMPLATE="centos-5-x86"
CONFIGFILE="vps.basic"

这样就可以通过下面的命令快速建立VPS

1
2
3
4
5
6
# vzctl create 101
Creating VPS private area: /vz/private/101
VPS is mounted
Postcreate action done
VPS is unmounted
VPS private area was created

4. 设置VPS
创建虚机后,使用下面命令来设置虚机的参数

1
2
3
4
vzctl set 101 --hostname test101.my.org --save  #设置主机名
vzctl set 101 --nameserver 202.96.209.5 --save  #设置DNS
vzctl set 101 --ipadd 172.1.1.101 --save  #设置IP
vzctl set 101 --userpasswd username:password #设置帐号

5. 启动和终止
a) 启动

1
vzctl start 101

b)终止

1
vzctl stop 101

c)查看状态

1
vzctl status 101

d)查看所有虚机的资源占用情况

1
cat /proc/vz/veinfo

e) 查看所有虚机的状态

1
vzlist -a

6. 删除VPS

1
vzctl destroy 101
8 Comments :, , more...

升级内核后spawn-fcgi无法启动

by on 九.17, 2009, under Linux, PHP

今天升级Linux内核到2.6.30-r5。但是升级重启后,发现blog打不开了。Nginx报502的Gateway错误。

第一反应就是起到php解析作用的spawn-fcgi没有启动起来。于是尝试再次手动启动。 但是发现怎么也启动不起来。

开始以为是因为升级内核引起的,于是退回以前的内核版本,结果一样。始终无法启动spawn-fcgi的fastcgi服务。说明不是内核的问题。 检查Nginx日志和系统日志都没有什么有价值的信息。

在一筹莫展的时候,突然注意到启动spawn-fcgi的命令中包含了php-cgi这个命令。 会不会是php的问题呢。 后来手工直接执行,发现确实php有问题。 重新使用emerge -av php编译安装php后,终于可以启动spawn-fcgi了。 网站也一切正常了。

出现问题的原因应该是以前使用emerge升级过系统,部分升级影响了php所需要调用的文件,当时没有重新启动,升级后的影响没有马上生效。 这次升级内核重启后,那些升级都生效了,结果问题就暴露出来了。

以后出现问题还是不要着急,任何环节都有可能出现问题。 而且有时候可能被问题的表象所蒙蔽,比如这次升级就误认为是因为内核的问题,但是其实后来证明根本跟内核没有关系。 出现问题还是要不放过任何可能出现问题的地方,即使你觉得不可能出现问题的地方也要去测试一下,逐一排查,最终肯定可以找到问题的原因的。

Gentoo下安装PHP和Nginx请参考之前发表的帖子

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...