作业管理
1,&
将命令放到后台执行
[root@centos1 ~]# tar -zpcf f\{1-5\}.txt f\[1\,3\].txt f5.txt  test &
[1] 18574
[root@centos1 ~]# pwd
/root
[1]+  Done                    tar -zpcf f\{1-5\}.txt f\[1\,3\].txt f5.txt test
[root@centos1 ~]#
在后台中执行的命令,如果有stdout和stderr时,他的数据依据输出到屏幕上,所以,我们会无法看到提示符,特忽悠无法完好地控制前台作业。所以,最好是使用数据流重定向。
例如:
tar -zpcvf /tmp/otc.tar.gz /etc > /tmp/log.txt 2>&1 &
暂停某个作业 ctrl+z
例如vi下按下ctrl+z会将作业放到后台
# --- default gateway
        option routers                  192.168.1.1;
        option subnet-mask              255.255.255.0;
        option nis-domain               "domain.org";
        option domain-name              "domain.org";
        option domain-name-servers      192.168.1.51,192.168.1.52;
        option time-offset              -18000; # Eastern Standard Time
#       option ntp-servers              192.168.1.1;
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
#       option netbios-node-type 2;
        range dynamic-bootp 192.168.1.2 192.168.1.100;
        default-lease-time 21600;
        max-lease-time 43200;
        # we want the nameserver to appear at a fixed address
        host ns {
                next-server marvin.redhat.com;
                hardware ethernet 12:34:56:78:AB:CD;
                fixed-address 207.175.42.254;
        }
}
~                                                                                                  
~                                                                                                  
"/etc/dhcpd.conf" 31L, 864C
[1]+  Stopped                 vi /etc/dhcpd.conf
[root@centos1 ~]#
观察当前后台作业的状态 jobs
[root@centos1 ~]# jobs
[1]+  Stopped                 vi /etc/dhcpd.conf
如果还想知道作业号的pid号码可以加上-l参数
[root@centos1 ~]# jobs -l
[1]- 18725 Stopped                 vi /etc/dhcpd.conf
[2]+ 18860 Stopped (tty output)    vim /etc/named.caching-nameserver.conf
+表示默认作业,既输入fg时会被拿到前台处理
将作业在后台执行:bg
ctrl+z可以将当前作业放到后台暂停,用bg命令则可以让作业在后台运行
将后台作业拿到前台处理:fg %jobnumber 百分号可有可无
管理后台作业:kill
参数:-l,列出当前kill能够使用的信号
      -1,重新读取一次参数的设置文件
      -2,同键盘输入ctrl+c
      -9,强制删除一个作业
      -15,正常终止一个作业
进程管理
进程的观察
ps
参数: -A 所有进程均显示出来
       -a 与终端无关的所有进程
       -u 有效用户的相关进程
       -x 列出完整信息
输出格式:
        l 较长、较详细地显示该pid信息
        j 作业的格式
       -f 更为完整的输出
列出当前属于自己这次登录的pid相关信息
[root@centos1 ~]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  4706  4704  0  75   0 -  1442 wait   pts/0    00:00:00 bash
4 R     0  4908  4706  0  77   0 -  1329 -      pts/0    00:00:00 ps
相关信息意义如下
F       进程的标志,4表示用户为超级用户
S       表示这个进程的状态
PID     进程ID,下面的PPID为父进程ID
C       CPU使用的资源百分百
PRI     Priority(优先级)的缩写
NI      Nice值
ADDR    该进程在内存的哪一部分,如果是运行的进程一般就是“-”
SZ      用掉的内存大小
WCHAN   当前进程是否在运行,若为-表示正在运行
TTY     登录者的终端位置
TIME    用掉的CPU时间
CMD     所执行的命令
与cron服务有关的pid号码
[root@centos1 ~]# ps aux | egrep cron 
root      3638  0.0  0.4   6400  1108 ?        Ss   07:04   0:00 crond
root      3690  0.0  0.2   1676   512 ?        SNs  07:04   0:00 anacron -s
root      5310  0.0  0.2   5020   660 pts/0    R+   07:57   0:00 egrep cron
列出所有当前在内存中的进程
[root@centos1 ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2   2072   660 ?        Ss   07:03   0:00 init [3]                         
root         2  0.0  0.0      0     0 ?        S<   07:03   0:00 [migration/0]
root         3  0.0  0.0      0     0 ?        SN   07:03   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S<   07:03   0:00 [watchdog/0]
root         5  0.0  0.0      0     0 ?        S<   07:03   0:00 [events/0]
root         6  0.0  0.0      0     0 ?        S<   07:03   0:00 [khelper]
root         7  0.0  0.0      0     0 ?        S<   07:03   0:00 [kthread]
........中间省略......
dbus      3325  0.0  0.3   2756   884 ?        Ss   07:04   0:00 dbus-daemon --system
root      3339  0.0  0.2   2172   764 ?        Ss   07:04   0:00 /usr/sbin/hcid
root      3350  0.0  0.1   1748   504 ?        Ss   07:04   0:00 /usr/sbin/sdpd
root      3362  0.0  0.0      0     0 ?        S<   07:04   0:00 [krfcommd]
root      4706  0.0  0.5   5772  1492 pts/0    Ss   07:36   0:00 -bash
root      5383  0.0  0.3   5360   936 pts/0    R+   07:59   0:00 ps aux
其中
USER(进程属于哪个用户)       PID %CPU %MEM    VSZ(使用的虚拟内存量KB)   RSS(占有固定内存量) TTY(进程在那个终端运行,与终端无关则显示?)      STAT(进程状态) START(进程启动时间)   TIME(使用cpu运行的时间) COMMAND(进程的时间命令)
top
参数  -d  后面接秒数,默认5秒
      -b  以批处理的方式执行top
      -n  与-b搭配
      -p  制定某个PID来进行观察
在top执行过程中可以使用的按键命令
       ? 显示在top中可以输入的按键命令
       p  按cpu的使用资源排序
       M  按内存的使用资源排序
       N  按PID来排序
       T  按进程使用的床铺时间累积排序
       K  给某个PID一个信号
       r  给某个PID重新确定一个值
范例:将top执行两次,结果输出到/tmp/top.txt
top -b -n 2 > /tmp/top.txt
仅查看pid为1的进程
top -d 2 -p1