cd命令小技巧

(1)从任一目录回到用户的home目录:“cd”或“cd ~”。举例如下:

[root@localhost ~]# cd /home/iso/
[root@localhost iso]# pwd
/home/iso
[root@localhost iso]# cd
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/iso/
[root@localhost iso]# pwd
/home/iso
[root@localhost iso]# cd ~
[root@localhost ~]# pwd
/root

(2)回到之前的工作目录:“cd -”。举例如下:

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd -
/home/iso
[root@localhost iso]# pwd
/home/iso

Bash shell内置wait命令简介

Bash shell内置了wait命令,官方文档对wait解释如下:

wait

     wait [-n] [jobspec or pid …]

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If the -n option is supplied, wait waits for any job to terminate and returns its exit status. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.

wait命令可以使当前shell进程挂起,等待所指定的由当前shell产生的子进程退出后,wait命令才返回。wait命令的参数可以是进程ID或是job specification。举例如下:

root# sleep 10 &
[3] 876
root# wait 876
[3]+  Done                    sleep 10
root# sleep 20 &
[1] 877
root# wait %1
[1]+  Done                    sleep 20 

wait命令一个很重要用途就是在Bash shell的并行编程中,可以在Bash shell脚本中启动多个后台进程(使用&),然后调用wait命令,等待所有后台进程都运行完毕,Bash shell脚本再继续向下执行。像下面这样:

command1 &
command2 &
wait

Bash shell还有一个内置变量:$!,用来记录最后一个被创建的后台进程。

root# sleep 20 &
[1] 874
root# sleep 10 &
[2] 875
root# echo $!
875

echo $!输出结果是875,是第二个执行的sleep命令。

参考资料:
Does bash script wait for one process to finish before executing another?

docker笔记(1)—— RHEL 7.0安装docker

官方RHEL 7.0安装docker的文档在这里。由于这个需要用户注册,所以在这里我介绍另一种方法:使用CentOSdocker rpm包。

(1)CentOS的软件包在这里:http://cbs.centos.org/repos/virt7-testing/x86_64/os/,你可以配置到yum源(软件仓库)的配置文件里,类似这样:

[centos-extra]
name=centos extra
baseurl=http://cbs.centos.org/repos/virt7-testing/x86_64/os/
enabled=1
gpgcheck=0

(2)运行“yum install docker”命令。

(3)安装成功后,运行“docker version”命令:

[root@localhost yum.repos.d]# docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.3.3
Git commit (client): a8a31ef/1.5.0
OS/Arch (client): linux/amd64
FATA[0000] Get http:///var/run/docker.sock/v1.17/version: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?

可以看到有“FATA[0000]......”提示,原因是没有启动docker daemon程序,使用“service docker start”可以启动docker程序。再次执行“docker version”命令:

[root@localhost bin]# docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.3.3
Git commit (client): a8a31ef/1.5.0
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.3.3
Git commit (server): a8a31ef/1.5.0

可以看到“FATA[0000]......”提示没有了。

(4)接下来的步骤可参考这里

Perf笔记(二)

Perf_events所处理的hardware event(硬件事件)需要CPU的支持,而目前主流的CPU基本都包含了PMUPerformance Monitoring Unit,性能监控单元)。PMU用来统计性能相关的参数,像cache命中率,指令周期等等。由于这些统计工作是硬件完成的,所以CPU开销很小。

X86体系结构为例,PMU包含了两种MSRsModel-Specific Registers,之所以称之为Model-Specific,是因为不同modelCPU,有些register是不同的):Performance Event Select RegistersPerformance Monitoring CountersPMC)。当想对某种性能事件(performance event)进行统计时,需要对Performance Event Select Register进行设置,统计结果会存在Performance Monitoring Counter中。

perf_events工作在采样模式(samplingperf record命令即工作在这种模式)时,由于采样事件发生时和实际处理采样事件之间有时间上的delay,以及CPU流水线和乱序执行等因素,所以得到的指令地址IP(Instruction Pointer)并不是当时产生采样事件的IP,这个称之为skid。为了改善这种状况,使IP值更加准确,Intel使用PEBSPrecise Event-Based Sampling),而AMD则使用IBSInstruction-Based Sampling)。

PEBS为例:每次采样事件发生时,会先把采样数据存到一个缓冲区中(PEBS buffer),当缓冲区内容达到某一值时,再一次性处理,这样可以很好地解决skid问题。

执行一下perf list --help命令,会看到下面内容:

The p modifier can be used for specifying how precise the instruction address should be. The p modifier can be specified multiple times:

       0 - SAMPLE_IP can have arbitrary skid
       1 - SAMPLE_IP must have constant skid
       2 - SAMPLE_IP requested to have 0 skid
       3 - SAMPLE_IP must have 0 skid

For Intel systems precise event sampling is implemented with PEBS which supports up to precise-level 2.

现在可以理解,经常看到的类似“perf record -e "cpu/mem-loads/pp" -a”命令中,pp就是指定IP精度的。

Perf笔记(一)

Perf_events是目前在Linux上使用广泛的profiling/tracing工具,除了本身是内核(kernel)的组成部分以外,还提供了用户空间(user-space)的命令行工具(“perf”,“perf-record”,“perf-stat”等等)。

perf_events提供两种工作模式:采样模式(sampling)和计数模式(counting)。“perf record”命令工作在采样模式:周期性地做事件采样,并把信息记录下来,默认保存在perf.data文件;而“perf stat”命令工作在计数模式:仅仅统计某个事件发生的次数。

我们经常看到类似这样的命令:“perf record -a ...... sleep 10”。在这里,“sleep”这个命令相当于一个“dummy”命令,没有做任何有意义的工作,它的作用是让“perf record”命令对整个系统进行采样,并在10秒后自动结束采样工作。

Unix/Linux命令行小技巧(20)- 按目录size大小列举目录

使用“du --block-size=kB | sort -n”或“du --block-size=kB | sort -nr”命令可以按目录size从小到大或从大到小列举目录。
举个例子:

[root@localhost /]$ du --block-size=kB | sort -n
 0kB    ./dev/bsg
 0kB    ./dev/bus
......
[root@localhost /]$ du --block-size=kB | sort -nr
 1179418kB    .
 937862kB    ./usr
......

技巧出处:https://twitter.com/nixcraft/status/290924082088775681

Unix/Linux命令行小技巧(19)- 显示系统的内存使用

使用“ps -A --sort -rss -o pid,comm,pmem,rss | less”命令可以显示系统的内存使用。
举个例子:

[root@home]$ ps -A --sort -rss -o pid,comm,pmem,rss | less
 PID COMMANDMEM  RSS
1386 abrtd           0.1   4824
1223 hald            0.0   3888
1423 login           0.0   3420
......

可以看到打印了每个进程占用的内存百分比,以及RSS的大小。
技巧出处:https://twitter.com/nixcraft/status/288158831551332353

Unix/Linux命令行小技巧(18)- ls命令按文件size大小列举文件

使用“ls -l -S *.d”命令按文件size从大到小列举文件,而“ls -l -S *.d | sort -k 5 -n”按文件size从小到大列举文件:
举个例子:

Nans-MacBook-Pro:Proc nanxiao$ ls -l -S *.d
-rwxr-xr-x@ 1 nanxiao  staff  4988 Dec  7 11:51 crash.d
-rwxr-xr-x@ 1 nanxiao  staff  3067 Dec  7 11:51 shortlived.d
-rwxr-xr-x@ 1 nanxiao  staff  2491 Dec  7 11:51 rwbytype.d
-rwxr-xr-x@ 1 nanxiao  staff  2452 Dec  7 11:51 pathopens.d
-rwxr-xr-x@ 1 nanxiao  staff  2263 Dec  7 11:51 stacksize.d
-rwxr-xr-x@ 1 nanxiao  staff  1684 Dec  7 11:51 kill.d
-rwxr-xr-x@ 1 nanxiao  staff  1656 Dec  7 11:51 sigdist.d
-rwxr-xr-x@ 1 nanxiao  staff  1651 Dec  7 11:51 threaded.d
-rwxr-xr-x@ 1 nanxiao  staff  1583 Dec  7 11:51 mmapfiles.d
-rwxr-xr-x@ 1 nanxiao  staff  1502 Dec  7 11:51 rwbbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1494 Dec  7 11:51 rwbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1476 Dec  7 11:51 sysbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 pidpersec.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 syscallbypid.d
-rwxr-xr-x@ 1 nanxiao  staff   283 Dec  7 11:51 filebyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   281 Dec  7 11:51 creatbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   268 Dec  7 11:51 writedist.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 newproc.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 syscallbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   264 Dec  7 11:51 readdist.d
-rwxr-xr-x@ 1 nanxiao  staff   259 Dec  7 11:51 writebytes.d
-rwxr-xr-x@ 1 nanxiao  staff   255 Dec  7 11:51 readbytes.d

Nans-MacBook-Pro:Proc nanxiao$ ls -l -S *.d | sort -k 5 -n
-rwxr-xr-x@ 1 nanxiao  staff   255 Dec  7 11:51 readbytes.d
-rwxr-xr-x@ 1 nanxiao  staff   259 Dec  7 11:51 writebytes.d
-rwxr-xr-x@ 1 nanxiao  staff   264 Dec  7 11:51 readdist.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 newproc.d
-rwxr-xr-x@ 1 nanxiao  staff   267 Dec  7 11:51 syscallbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   268 Dec  7 11:51 writedist.d
-rwxr-xr-x@ 1 nanxiao  staff   281 Dec  7 11:51 creatbyproc.d
-rwxr-xr-x@ 1 nanxiao  staff   283 Dec  7 11:51 filebyproc.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 pidpersec.d
-rwxr-xr-x@ 1 nanxiao  staff  1331 Dec  7 11:51 syscallbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1476 Dec  7 11:51 sysbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1494 Dec  7 11:51 rwbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1502 Dec  7 11:51 rwbbypid.d
-rwxr-xr-x@ 1 nanxiao  staff  1583 Dec  7 11:51 mmapfiles.d
-rwxr-xr-x@ 1 nanxiao  staff  1651 Dec  7 11:51 threaded.d
-rwxr-xr-x@ 1 nanxiao  staff  1656 Dec  7 11:51 sigdist.d
-rwxr-xr-x@ 1 nanxiao  staff  1684 Dec  7 11:51 kill.d
-rwxr-xr-x@ 1 nanxiao  staff  2263 Dec  7 11:51 stacksize.d
-rwxr-xr-x@ 1 nanxiao  staff  2452 Dec  7 11:51 pathopens.d
-rwxr-xr-x@ 1 nanxiao  staff  2491 Dec  7 11:51 rwbytype.d
-rwxr-xr-x@ 1 nanxiao  staff  3067 Dec  7 11:51 shortlived.d
-rwxr-xr-x@ 1 nanxiao  staff  4988 Dec  7 11:51 crash.d

技巧出处:https://twitter.com/nixcraft/status/290535134485176321

Unix/Linux命令行小技巧(17)- 打印进程打开的文件数目

使用“for p in $(pidof process); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opened."; done”命令打印process进程打开的文件数目。
举个例子:

[root@localhost /]# for p in $(pidof java); do echo "PID # $p has $(lsof -n -a -p $p|wc -l) fd opened."; done
PID # 37747 has 139 fd opened.
PID # 30279 has 117 fd opened.
PID # 28033 has 204 fd opened.
PID # 10501 has 211 fd opened.
PID # 4998 has 442 fd opened.

显示了所有java进程打开的文件数目。
技巧出处:https://twitter.com/nixcraft/status/281010856706334720