[root@daixuan init.d]# ls /var/log/messages                                     查看Linux日志文件
/var/log/messages
[root@daixuan init.d]# cat /etc/logrotate.conf                                  查看日志切割的配置文件
# see "man logrotate" for details
# rotate log files weekly
weekly                                                                                              每周切割一次
# keep 4 weeks worth of backlogs                                                   
rotate 4                                                                                            只保留4个日志文件
# create new (empty) log files after rotating old ones
create                                                                                               生成新的
# use date as a suffix of the rotated file
dateext                                                                                             以日期形式命名
# uncomment this if you want your log files compressed
#compress                                                                                       #未打开,不压缩日志
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly                                                                                       wtmp按月归档
    create 0664 root utmp                                                                 权限664,属主root,属组utmp
        minsize 1M                                                                               最小大小1M,小于1M就不进行切割
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}
# system-specific logs may be also be configured here. 

[root@daixuan init.d]# ls /etc/logrotate.d                                        相关的日志
cups  dracut  ppp  syslog  wpa_supplicant  yum
[root@daixuan init.d]# cat /etc/logrotate.d/syslog                            
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts                                                                                表示在切割的配置文件中还可以使用一些命令
    postrotate                                                                                     切割完成之后进行的一些操作
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript                                                                                       kill -HUP重新加载生成新的日志
}
[root@daixuan init.d]# ps aux                                                          查看进程
[root@daixuan init.d]# kill pid                                                          杀死进程
[root@daixuan init.d]# kill -9 pid                                                      强制杀死进程
[root@daixuan init.d]# killall crond                                                   killall+名称 杀死服务
[root@daixuan init.d]# killall -9 crond                                               强制killall+名称 杀死服务
[root@daixuan init.d]# tail /var/log/messages                                  查看系统内核相关的日志,包含重启,yum安装的日志等信息
Nov 20 09:40:22 daixuan ntpd[2158]: 0.0.0.0 c618 08 no_sys_peer
Nov 20 09:40:26 daixuan ntpd_intres[2311]: DNS 2.centos.pool.ntp.org -> 218.189.210.4
Nov 20 09:40:26 daixuan ntpd_intres[2311]: DNS 3.centos.pool.ntp.org -> 59.149.185.193
Nov 20 11:52:13 daixuan yum[3578]: Installed: ntsysv-1.3.49.3-5.el6.i686
Nov 20 13:51:13 daixuan ntpd[2158]: 0.0.0.0 0628 08 no_sys_peer
Nov 20 14:21:47 daixuan ntpd[2158]: 0.0.0.0 0613 03 spike_detect -45.894666 s
Nov 20 14:25:58 daixuan ntpd[2158]: 0.0.0.0 061c 0c clock_step -45.882780 s
Nov 20 14:25:58 daixuan ntpd[2158]: 0.0.0.0 0615 05 clock_sync
Nov 20 14:25:59 daixuan ntpd[2158]: 0.0.0.0 c618 08 no_sys_peer
Nov 20 14:56:47 daixuan kernel: hrtimer: interrupt took 10699930 ns

[root@daixuan init.d]# ls /var/log/wtmp                                         查看登录历史的命令
/var/log/wtmp
[root@daixuan init.d]# last                                                             用last调用wtmp日志文件,查看登录的历史信息
root     pts/0        192.168.101.17   Fri Nov 20 11:17   still logged in
reboot   system boot  2.6.32-573.el6.i Thu Nov 19 22:09 - 15:00  (16:50)
root     pts/0        192.168.101.17   Thu Nov 19 16:39 - 18:21  (01:42)
root     tty1                          Thu Nov 19 16:36 - down   (01:45)
root     pts/0        192.168.101.17   Thu Nov 19 15:47 - 16:38  (00:50)
root     pts/0        192.168.100.13   Thu Nov 19 12:08 - 15:04  (02:56)

[root@daixuan init.d]# ls /var/log/btmp                                          无效的登录历史
/var/log/btmp
[root@daixuan init.d]# lastb                                                            lastb调用的是lastb,查看无效的登录历史
root     ssh:notty    192.168.101.17   Mon Nov 16 08:26 - 08:26  (00:00)
root     ssh:notty    192.168.101.112  Sun Nov  8 19:30 - 19:30  (00:00)
rooot    ssh:notty    192.168.101.137  Thu Nov  5 14:16 - 14:16  (00:00)
btmp begins Thu Nov  5 14:16:27 2015

[root@daixuan init.d]# ls /var/log/maillog                                       邮件相关的
[root@daixuan init.d]# ls /var/log/secure
[root@daixuan init.d]# tail /var/log/secure                                      关于验证相关的,正常或者失败的登录记录
Nov 19 16:38:44 daixuan sshd[3329]: pam_unix(sshd:session): session closed for user root
Nov 19 16:39:00 daixuan sshd[3759]: Accepted password for root from 192.168.101.17 port 61168 ssh2

[root@daixuan init.d]# ls /var/log/dmesg                                       系统启动过程中硬件相关的日志,历史的记录
[root@daixuan init.d]# cat /var/log/dmesg
[root@daixuan init.d]# dmesg                                                        实时更新的,可以有助于我们获得一些硬件相关的错误信息

总结:
1. /var/log/messages:系统里面大部分日志都是在/var/log/messages里面,查问题时,这个日志是不能忽略的,比如内核相关的问题,当你的机器访问量特别大时,这个日志里面就会记录一些内核异常的错误。
2. /var/log/wtmp:wtmp这个是查看登录历史的,last 命令看就是这个日志
3. /var/log/btmp:btmp是查看登录历史中没有正确登录的那些ip,用lastb查看
4. /var/log/maillog:maillog 当然是记录邮件相关日志的,当你发邮件不成功,不正常时就看这个日志
5./var/log/secure: secure 是安全相关的日志,比如我们以后讲到ftp不能正常登录时,就是看这个日志
6. /var/log/dmesg:dmesg 这个日志是系统开机时,记录的一些硬件信息,很少看这个日志


xargs和exec详解

xargs使用i选项后,加另一条命令 -exec相当于find的一条选项
[root@daixuan init.d]# find /var/log/ -type f -mtime +10                                    找到、var/log/下10天以前的文件
/var/log/messages-20151025
/var/log/gdm/:0-slave.log.4
/var/log/gdm/:1-slave.log
[root@daixuan init.d]# find /var/log -type f -mtime +10 -exec cp {} {}.bak \;     找到、var/log/下10天以前的文件复制并且重命名为.bak文件,-exec 加命令表示进行操作,{}表示前面找到的文件, 脱意符号,脱意“;”,否则认为 \之前为一条命令 ,-exec 是find的一个选项而已
[root@daixuan log]# find /var/log -type f -mtime +10| xargs -i cp {} {}.1           | 管道xargs+ 一条命令
[root@daixuan log]# ls Xorg.1.log
Xorg.1.log      Xorg.1.log.1    Xorg.1.log.bak
[root@daixuan ~]# ls x*
xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj  xak  xal                                        
[root@daixuan ~]# ls x* | xargs rm                                                                    找到x开头的文件并且删除这些文件  
[root@daixuan ~]# ls x*
[root@daixuan ~]# ls *.txt | xargs -i cp {} {}.bak                                               把当前目录下的txt结尾的文件复制并重命名为.txt.bak的文件
[root@daixuan ~]# ls *.txt*
1234.txt     #1.txt.bak  3.txt.bak    b.txt.bak  test.txt.bak
注:
1、xargs加-i是因为管道前面的结果,也就是文件有很多,而且我们要操作的是类似, cp, mv这样,必须要有两个参数才可以,假如是rm,xargs就不用加-i
2、管道前面如果是文件,则需要xargs,如果是字符串,用命令
3、-name "*bak" 当遇到通配时,需要加双引号


screen命令
nohup命令,丢入后台的同时不会出现中断,会收集进程在运行中产生的日志
[root@daixuan 111]# nohup sleep 100&                                               使用nohup避免因为断电、系统关闭、终端异常导致的进程中断
[1] 5056
[root@daixuan 111]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@daixuan 111]# cat nohup.out
[root@daixuan 111]# jobs
[1]+  Running                 nohup sleep 100 &
[root@daixuan 111]# yum install -y screen
[root@daixuan 111]# screen                                                               直接进入虚拟后台终端
[root@daixuan 111]#top                                                                    在虚拟终端下运行top命令,Ctrl+a+d 退出虚拟终端,top仍在后天运行
[root@daixuan 111]# screen -ls                                                          screen -ls 查看后台运行的命令
There is a screen on
        5102.pts-0.daixuan      (Detached)
1 Socket in /var/run/screen/S-root.

[root@daixuan 111]# screen[root@daixuan 111]# vmstat 1                                                               ctrl+a+d
[root@daixuan 111]# ps aux | grep -E 'top|vmstat'
root      5113  0.2  0.0   2692  1128 pts/1    S+   17:35   0:01 top         pts1和pts2都是虚拟出来的终端
root      5171  0.0  0.0   2152   604 pts/2    S+   17:42   0:00 vmstat 1

[root@daixuan 111]# ps aux | grep screen                                             screen的进程看不到后台进程
root      5185  0.0  0.0   6048   780 pts/0    S+   17:45   0:00 grep screen

[root@daixuan 111]# screen -ls
There are screens on:
        5160.pts-0.daixuan      (Detached)
        5102.pts-0.daixuan      (Detached)
2 Sockets in /var/run/screen/S-root.
[root@daixuan 111]# screen -r 5160                                                     -r 5160    进入指定的screen,Ctrl+d 真正断开screen,screen中输入exit
[root@daixuan 111]# screen -r 5160 
[root@daixuan ~]# screen -S test3                                                       -S指定screen的后台命令ID的名称
[detached]
[root@daixuan ~]# screen -ls
There are screens on:
        5256.test1      (Detached)
        5283.test3      (Detached)
[root@daixuan ~]# screen -r test3                进入screen test3,使用-r选项