Linux进程管理及while循环

目录

  • 进程的相关概念
  • 进程查看及管理工具的使用
  • Linux系统作业控制
  • 调整进程优先级
  • 网络客户端工具
  • bash之while循环

20.1、进程类型

守护进程

daemon,在系统引导过程中启动的进程;跟终端无关的进程;

前台进程

跟终端相关,通过终端启动的进程;也可以把前台启动的进程送给后台,以守护模式运行;

20.2、进程状态

  1. 运行态:running

  2. 就绪态:ready

  3. 睡眠态:

    可中断:interruptable

    不可中断:uninterruptible

  4. 停止态:暂停于内存中,但不会被调度执行,除非手动启动;stopped;

  5. 僵死态:zommbie;

20.3、pstree命令

功用:进程树查看工具;

注意:最小化安装centos后默认没有安装pstree命令,需要安装psmisc软件包,包含pstree, fuser, killall三个命令

示例

[root@bj-1-141 ~]# pstree
systemd─┬─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─atd
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon
        ├─irqbalance
        ├─login───bash
        ├─master─┬─cleanup
        │        ├─local
        │        ├─pickup
        │        ├─qmgr
        │        └─trivial-rewrite
        ├─nginx───4*[nginx]
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd─┬─sshd───bash───htop
        │      └─sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─wpa_supplicant

20.4、ps命令

功用:取出当前系统上命令运行进程的状态;

使用格式

ps [OPTIONS]

常用选项组合之一 (aux)

a:所有与终端相关的进程;
x:与终端无关的进程;
u:以用户为中心来组织进程状态信息显示;

示例:

[root@bj-1-141 ~]# ps aux|less
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.4 123908  4568 ?        Ss   1月02   0:37 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2  0.0  0.0      0     0 ?        S    1月02   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    1月02   0:01 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   1月02   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    1月02   0:01 [migration/0]
root          8  0.0  0.0      0     0 ?        S    1月02   0:00 [rcu_bh]

ps aux输出各字段释义

USER	PID		%CPU %MEM	VSZ	RSS TTY		STAT START	TIME	COMMAND
# 各字段表示含义为
USER:进程的用户
PID:进程号
%CPU:CPU占用比率
%MEN:内存占用比率
VSZ:虚拟内存集,占用大小
RSS:常驻内存集
TTY:终端
STAT:状态
	R:运行态;
	S:可中断睡眠;
	D:不可中断睡眠;
	T:停止态;
	Z:僵死态;
	+:前台进程;
	l:多线程进程;
	N:低优先级进程;
	<:高优先级进程
	s: session leader;
START:进程的发起时间;
TIME:累计运行时长;
COMMAND:执行的命令;

常用选项组合之二 (-ef)

-e:显示所有进程;
-f:显示完整格式的进程信息;

示例:

[root@bj-1-141 ~]# ps -ef|less
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 1月02 ?       00:00:37 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0 1月02 ?       00:00:00 [kthreadd]
root          3      2  0 1月02 ?       00:00:01 [ksoftirqd/0]
root          5      2  0 1月02 ?       00:00:00 [kworker/0:0H]
root          7      2  0 1月02 ?       00:00:01 [migration/0]

ps -ef 输出各字段释义

UID         PID   PPID  	C 				STIME 		TTY          TIME 		CMD
用户		进程号  父进程号	CPU占用百分比	启动时间	与其相关的终端	累计运行时间	启动此进程的命令

常用选项组合之三 ( -eFH )

-F:显示完整格式的进程信息;
	PSR:运行与哪颗CPU上;
-H:以层级结构显示进程的相关信息;

示例:

[root@bj-1-141 ~]# ps -eFH|less
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root       2153      1  0 22781  2172   0 1月02 ?       00:00:09   /usr/libexec/postfix/master -w
postfix    2155   2153  0 22854  4152   3 1月02 ?       00:00:03     qmgr -l -t unix -u
postfix   33730   2153  0 22807  3896   1 22:53 ?        00:00:00     pickup -l -t unix -u
postfix   34355   2153  0 22844  3960   1 23:56 ?        00:00:00     cleanup -z -t unix -u
postfix   34356   2153  0 22808  3908   2 23:56 ?        00:00:00     trivial-rewrite -n rewrite -t unix -u
postfix   34358   2153  0 22833  4528   0 23:56 ?        00:00:00     local -t unix
root      34166      1  0 30557  2216   2 23:35 ?        00:00:00   nginx: master process /usr/sbin/nginx
nginx     34167  34166  0 30664  3080   3 23:35 ?        00:00:00     nginx: worker process
nginx     34168  34166  0 30664  3232   2 23:35 ?        00:00:00     nginx: worker process
nginx     34169  34166  0 30664  3248   2 23:35 ?        00:00:00     nginx: worker process
nginx     34170  34166  0 30664  3280   0 23:35 ?        00:00:00     nginx: worker process

常用选项组合之四 ( -eo | axo )

o field1,field2,...:自定义要显示的字段列表,以逗号分隔;
# 常用的field
pid, ni, pri, pcpu, stat, comm, tty, ppid, rtprlo
ni:nice值
priority:优先级;
trprlo:实时优先级;

注意:o必须写在最后面;

示例

[root@bj-1-141 ~]# ps axo pid,comm,ni,priority|grep nginx
 34166 nginx             0  20
 34167 nginx             0  20
 34168 nginx             0  20
 34169 nginx             0  20
 34170 nginx             0  20

20.5、pgrep,pkill命令

功用:根据进程名查询或杀死进程;

使用格式

pgrep [OPTIONS] pattern

常用选项

-u uid: 显示指定用户的相关进程;
-U uid:显示指定用户的相关进程;
-t TERMINAL:与指定终端相关的进程;
-l:显示进程名;
-a:显示完整格式的进程名;
-P pid:显示此进程的子进程;

示例

练习1、找出当前系统上以nginx用户身份运行的进程;

[root@bj-1-141 ~]# pgrep -au nginx
34167 nginx: worker process
34168 nginx: worker process
34169 nginx: worker process
34170 nginx: worker process

练习2、找出当前系统上所有进程名包含nginx的进程;

[root@bj-1-141 ~]# pgrep -a nginx
34166 nginx: master process /usr/sbin/nginx
34167 nginx: worker process
34168 nginx: worker process
34169 nginx: worker process
34170 nginx: worker process

练习3、使用pkil命令结束nginx进程;

[root@bj-1-141 ~]# pkill nginx
您在 /var/spool/mail/root 中有新邮件
[root@bj-1-141 ~]# pgrep -a nginx

20.6、pidof命令

功用:根据进程名取出其PID;

示例

练习1、取出所有nginx进程的PID;

[root@bj-1-141 ~]# pidof nginx
34751 34750 34749 34748 34747

20.7、top命令

功用:显示Linux进程相关信息;

使用格式

top [OPTION]

常用选项

-d #:#表示数字,以指定刷新时间间隔,默认3秒刷新一次;
-b:以批次方式显示;
-n #:显示多少批次;

top内嵌命令

当我们执行top命令以后在内部可以使用如下命令来显示或排序;

# 排序方式
M:以占据内存百分比排序;
P:默认,以占据CPU百分比排序;
T:累计占用CPU时间排序;

# 首部信息显示或关闭
uptime:l命令显示或关闭;
tasks及CPU信息:t命令显示或关闭;
内存信息:m命令显示或关闭内存信息;

# 退出命令
q

# 修改刷新时间间隔
s 
# 终止指定的进程
k

top命令输出结果各字段释义

top - 07:12:41 up 1 day, 22:57,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  95 total,   1 running,  94 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.3%us,  0.3%sy,  0.0%ni, 99.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1004352k total,   752076k used,   252276k free,    63756k buffers
Swap:  1048572k total,        0k used,  1048572k free,   480436k cached

#07:12:41  当前系统时间
#up 1 day, 22:57   运行时长,操作系统启动多长时间
#2 users	登录的用户
#load average: 0.00, 0.00, 0.00  平均负载,过去的1分钟,5分钟,15分钟的负载情况
#Tasks:  95 total	总共运行的进程数‘
#1 running	一个运行的
#94 sleeping	94个睡眠的
#0 stopped	零个停止的
#0 zombie	零个僵死的

#Cpu(s):	CPU的占用率
#0.3%us		用户空间占用的百分比
#0.3%sy		系统占用CPU的百分比
#0.0%ni		调整nice值所占用的百分比;
#99.3%id	CPU空闲百分比
#0.0%wa		等待I/O完成所占用的时长,如果百分比很大说明硬盘太慢了
#0.0%hi		硬件中断所占用的时间
#0.0%si		软中断
#0.0%st		被虚拟化程序所占用的cpu百分比;

#Mem:	内存的使用率
#1004352k total		内存的总空间
#752076k used		已使用的内存空间
#252276k free		空闲内存空间
#63756k buffers		缓冲区空间

#Swap:	交换空间的使用率
#1048572k total		总空间
#0k used			占用的空间
#1048572k free		空闲的空间
#480436k cached		缓存的空间,是物理内存的cache

20.8、uptime命令

功用:显示系统时间、运行时长、以及过去1分钟,5分钟,15分钟的平均负载;

示例

[root@bj-1-141 ~]# uptime
 08:56:51 up 1 day, 10:47,  4 users,  load average: 0.01, 0.03, 0.05

20.9、htop命令

增强版的top命令

使用格式

htop [-dus]
-d #:指定延迟时间间隔;
-u username:仅显示以指定用户的进程;
-s CLOUME:以指定字段进行排序;

htop内部命令

l:显示选定的进程打开的文件列表;
s:跟踪选定的进程的系统调用;
t:以层级关系显示各进程状态;
a:将选定的进程绑定至某CPU核心;默认运行在所有核心,取消[x]里的x即可设置;

20.10、vmstat命令

功用:报告虚拟内存的统计数据;

使用格式

vmstat [OPTIONS] [delay [count]]
-s:显示内存统计数据信息;
[delay [count]]:表示间隔多长时间,一共打印几次;例如 vmstat 2 3

示例

[root@bj-1-141 ~]# vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 498532    876 366804    0    0     0     1   12   18  0  0 100  0  0
 0  0      0 498532    876 366812    0    0     0     0   39   62  0  0 100  0  0
 0  0      0 498532    876 366812    0    0     0     0   29   47  0  0 100  0  0
 0  0      0 498532    876 366812    0    0     0     0   41   62  0  0 100  0  0
 0  0      0 498532    876 366812    0    0     0     0   32   47  0  0 100  0  0

vmstat命令输出结果各字段释义

procs:
	r:等待运行的进程的个数;CPU上等待运行的任务队列长度;
	b:处于不可中断睡眠态的进程个数;被阻塞的任务队列的长度;
memory:
	swpd:交互内存的使用总量;
	free:空闲的物理内存总量;
	buffer:用于buffer的内存总量;
	cache:用于cache的内存总量;
swap:
	si:数据进入swap中的数据速率(kb/s);
	so:数据离开swap的速率(kb/s);
io:
	bi:从块设备读入数据到系统的速率(kb/s);
	bo:保存数据至块设备的速率(kb/s);
system:
	in:中断发生速率,每秒钟的中断数;
	cs:上下文切换,进程切换,速率,每秒钟切换的次数;
cpu:
	us:用户空间程序所占有CPU时间的百分比;
	sy:系统占用CPU时间的百分比;
	id:空闲的CPU百分比;
	wa:wait,等待io完成的;
	st:被虚拟化程序所占用的;

20.11、pmap命令

功用:报告进程的内存映射表;

使用格式

pmap [OPTIONS] pid [...]
-x:显示详细格式信息;

示例

[root@bj-1-141 ~]# pmap -x 34747
34747:   nginx: master process /usr/sbin/nginx
Address           Kbytes     RSS   Dirty Mode  Mapping
00007f85949af000      20       0       0 r-x-- nginx.so
00007f85949b4000    2044       0       0 ----- nginx.so
00007f8594bb3000       4       4       4 r---- nginx.so
00007f8594bb4000       4       4       4 rw--- nginx.so
00007f8594bb5000      68       0       0 r-x-- ngx_stream_module.so
00007f8594bc6000    2044       0       0 ----- ngx_stream_module.so
00007f8594dc5000       4       4       4 r---- ngx_stream_module.so
00007f8594dc6000       8       8       8 rw--- ngx_stream_module.so
00007f8594dc8000      84       0       0 r-x-- ngx_mail_module.so
00007f8594ddd000    2048       0       0 ----- ngx_mail_module.so
00007f8594fdd000       4       4       4 r---- ngx_mail_module.so
00007f8594fde000       8       8       8 rw--- ngx_mail_module.so

20.12、glances命令

功用:glances是用来监视Linux系统的工具,可以监视CPU,内存,平均负载,网络流量,磁盘I/O等利用情况;默认centos系统未安装此命令,使用yum -y install glances安装;

使用格式及常用选项

glances [OPTIONS]
-b:以byte为单位显示网卡数据速率;
-d:关闭磁盘i/o模块;
-m:关闭内存模块;
-n:关闭network模块;
-t #:刷新时间间隔;
-1:每个CPU的相关数据单独显示;
-o {HTML|CSV}:输出格式;
-f /PATH/TO/SOMEDIR:设定输出文件的保存目录;

glances内部使用的命令

  a  自动排序                         l  Show/hide logs
  c  根据CPU使用率排序                b  Bytes or bits for network I/O
  m  根据内存占用比排序               w  删除警告日志
  p  根据进程名字排序                 x  删除警告和严重级别的日
  i  Sort processes by I/O rate       1  全局CPU或者每个CPU的状态
  d  显示/隐藏硬盘I/O状态             h  Show/hide this help screen
  f  显示/隐藏文件系统状态            t  View network I/O as combination
  n  显示/隐藏network状态             u  View cumulative network I/O
  s  显示/隐藏sensors状态             z  Show/hide processes list
  y  显示/隐藏hddtemp状态             q  Quit (Esc and Ctrl-C also work)

20.12.1、C/S模式下运行glades命令

服务模式

glances -s -B IPADDR
IPADDR:本机某地址,用于监听;

客户端模式

glances -c IPADDR
IPADDR:为远程服务器的地址;

20.12.2、示例

练习1、每隔2秒刷新一次数据;

[root@bj-1-141 ~]# glances -t 2

练习2、单独显示每个CPU的数据;

[root@bj-1-141 ~]# cls                                                                                	      2017-01-04 09:30:52
[root@bj-1-141 ~]# glances -1
              bj-1-141.enzhi.com (CentOS Linux 7.2.1511 64bit / Linux 3.10.0-327.el7.x86_64)               Uptime: 1 day, 11:22:06

每CPU     0.3%    6.2%    0.3%    0.0%   Load   4核      Mem    22.9%  active:    230M   Swap    0.0%
user:     0.0%    3.4%    0.0%    0.0%   1 min:   0.08   total:  978M  inactive:  119M   total: 2.00G
system:   0.3%    2.8%    0.3%    0.0%   5 min:   0.04   used:   224M  buffers:   876K   used:      0
iowait:   0.0%    0.0%    0.0%    0.0%   15 min:  0.05   free:   754M  cached:    280M   free:  2.00G

练习3、配置glances服务端与客户端模式

# 启动服务端
[root@bj-1-160-enzhi ~]# glances -s -B 192.168.1.161
Glances服务器启动了 192.168.1.161:61209

# 客户端连接服务端
[root@bj-1-141 ~]# glances -c 192.168.1.161
                     bj-1-160-enzhi.com (CentOS 6.8 64bit / Linux 2.6.32-642.el6.x86_64)                    Uptime: 1 day, 8:17:35

CPU       2.4%                   Load   2核	 Mem    15.1%  active:    251M   Swap    0.0%
user:     0.9%  nice:     0.0%   1 min:   0.11   total:  981M  inactive: 86.9M   total: 2.00G
system:   1.4%  iowait:   0.0%   5 min:   0.09   used:   148M  buffers:  71.1M   used:      0
idle:    97.6%  irq:	  0.0%   15 min:  0.07   free:   833M  cached:    249M   free:  2.00G

20.13、dstat命令

功用:生成系统资源统计数据;安装dstat:yum -y install dstat

使用格式

dstat [-afv] [options...] [delay [count]]
[delay [count]]:
	delay:表示几秒钟显示一次;
	count:表示一共显示几次;

常用选项

-c:显示CPU相关信息;
-C #,#,total:显示指定某颗CPU或者总的;
-d:显示磁盘相关信息;
-D /dev/sd[a-z]:显示指定磁盘的相关信息;
-g:显示page相关速率数据;
-m:显示内存相关统计数据;
-n:显示网络相关的统计数据;
-p:显示进程相关的统计数据;
-r:显示I/O请求相关的数据;
-s:显示swap相关统计数据;
--tcp:enable tcp stats (listen, established, syn, time_wait, close)
--udp:enable udp stats (listen, active)
--raw:
--top-bio:显示最占用磁盘i/o的进程;
--top-cpu:显示最占用CPU的进程;
--top-mem:显示最占用内存的进程;
--top-lantency:显示延迟最大的进程;

示例

练习1、显示当前系统中占用CPU最多的进程,每2秒显示一次,共显示5次;

[root@bj-1-141 ~]# dstat --top-cpu 2 5
-most-expensive-
  cpu process
rcu_sched    0.0
kworker/0:2  0.1
kworker/0:2  0.1
rcu_sched    0.5
rcu_sched    0.4
rcu_sched    0.6

练习2、显示当前系统中tcp相关的进程;

[root@bj-1-141 ~]# dstat --tcp 2 5
----tcp-sockets----
lis act syn tim clo
  6   1   0   0   0
  6   1   0   0   0
  6   1   0   0   0
  6   1   0   0   0
  6   1   0   0   0
  6   1   0   0   0

练习3、显示/dev/sda磁盘相关的统计数据;

[root@bj-1-141 ~]# dstat -D /dev/sda 2 5
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- --dsk/sda-- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw
  0   0 100   0   0   0|1690B 2885B|   0     0 |   0     0 |  49    75
  0   0 100   0   0   0|   0     0 |  99B  590B|   0     0 |  62   101
  0   0 100   0   0   0|   0     0 |  66B  342B|   0     0 |  58    96
  0   0 100   0   0   0|   0     0 |  66B  342B|   0     0 |  55    89
  0   0 100   0   0   0|   0     0 |  66B  342B|   0     0 |  71   102
  0   0 100   0   0   0|   0     0 | 146B  342B|   0     0 |  63    95

20.14、kill命令

功用:用于向进程发送信号,以实现对进程的管理;

使用格式

# 查看当前系统可用信号
kill -l [SIGNAL]
	-l:显示当前系统可用信号;
# 发送信号
kill [-s SIGNAL] pid...

信号的标识方法

  1. 信号的数字标识
  2. 信号的完整名称
  3. 信号的简写名称( kill -l SIGNAL 查看)

常用信号

1)SIGHUP:无须关闭进程而让其重读配置文件;平滑重启;
2)SIGINT:终止正在运行的进程;相当于ctrl+c;
9)SIGKILL:杀死正在运行的程序;
15)SIGTERM:终止运行中的程序;正常关闭;

示例

练习1、平滑重启nginx服务;

[root@bj-1-141 ~]# systemctl start nginx.service
[root@bj-1-141 ~]# ps -ef|grep nginx
root      37541      1  0 11:19 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     37542  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37543  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37544  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37545  37541  0 11:19 ?        00:00:00 nginx: worker process
root      37547  34630  0 11:19 pts/2    00:00:00 grep --color=auto nginx
[root@bj-1-141 ~]# kill -HUP `cat /run/nginx.pid`
[root@bj-1-141 ~]# ps -ef|grep nginx
root      37541      1  0 11:19 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     37549  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37550  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37551  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37552  37541  0 11:19 ?        00:00:00 nginx: worker process

练习2、正常关闭nginx服务;

[root@bj-1-141 ~]# ps -ef|grep nginx
root      37513      1  0 11:18 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     37514  37513  0 11:18 ?        00:00:00 nginx: worker process
nginx     37515  37513  0 11:18 ?        00:00:00 nginx: worker process
nginx     37516  37513  0 11:18 ?        00:00:00 nginx: worker process
nginx     37517  37513  0 11:18 ?        00:00:00 nginx: worker process
root      37521  34630  0 11:18 pts/2    00:00:00 grep --color=auto nginx
您在 /var/spool/mail/root 中有新邮件
[root@bj-1-141 ~]# kill -TERM `cat /run/nginx.pid`
[root@bj-1-141 ~]# ps -ef|grep nginx
root      37527  34630  0 11:18 pts/2    00:00:00 grep --color=auto nginx

20.15、killall命令

功用:根据进程名杀死进程;

使用格式

killall [-SIGNAL] name

示例

[root@bj-1-141 ~]# ps -ef|grep nginx
root      37541      1  0 11:19 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     37549  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37550  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37551  37541  0 11:19 ?        00:00:00 nginx: worker process
nginx     37552  37541  0 11:19 ?        00:00:00 nginx: worker process
root      37588  34630  0 11:22 pts/2    00:00:00 grep --color=auto nginx
[root@bj-1-141 ~]# killall nginx
[root@bj-1-141 ~]# ps -ef|grep nginx
root      37594  34630  0 11:22 pts/2    00:00:00 grep --color=auto nginx

20.16、Linux系统作业控制

20.16.1、作业类型

前台作业:通过终端启动,且启动后会一直占据终端;

后台作业:可以通过终端启动,但启动后即转入后台运行(释放终端);

20.16.2、如何让作业运行于后台

运行中的作业

Ctrl+z:将前台的作业送往后台;
# 注意:送往后台后,作业会转为停止态;

尚未启动的作业

COMMAND &
# 注意:此类作业虽然被送往后台,但依然与其终端相关,如果希望把送往后台的作业剥离与终端的关系,使用下面命令方式;
nohup COMMAND &

查看所有的作业

jobs

20.16.3、可实现作业控制的常用命令

fg [[%] JOB_NUM]:把指定的作业调回前台;
bg [[%] JOB_NUM]:把送往后台的作业在后台继续运行;
kill [[%] JOB_NUM]:终止指定的作业,必须加%百分号;

示例

[root@bj-1-141 ~]# vim test.txt &
[1] 37680
[root@bj-1-141 ~]# jobs
[1]+  已停止               vim test.txt
[root@bj-1-141 ~]# kill % 1

[1]+  已停止               vim test.txt

20.17、进程优先级调整

可通过nice值调整的优先级范围:100-139;分别对应于:-20 , 19;数字越小优先级越高;进程启动是默认优先级为0,其优先级为120;

20.17.1、nice命令

功用:以指定的nice值启动并运行命令;

使用格式

nice [option] [COMMAND [ARGU]...]
-n #:指定nice值;

注意:仅管理员可调低nice值;

示例

练习1、调整htop命令的优先级为-5;

[root@bj-1-141 ~]# nice -n -5 htop
# 查看top进程的nice值是否为-5
[root@bj-1-141 ~]# ps axo pid,comm,ni,priority|grep htop
 37791 htop             -5  15

20.17.2、renice命令

功用:调整运行中命令的nice值;

使用格式

renice [-n] NICE PID ...

查看nice值和优先级:ps axo pid,comm,ni,priority

示例

练习、将运行中的htop命令的nice值调整为-5;

[root@bj-1-141 ~]# ps axo pid,comm,ni,priority|grep htop
 37810 htop              0  20
您在 /var/spool/mail/root 中有新邮件
[root@bj-1-141 ~]# renice -n -5 37810
37810 (进程 ID) 旧优先级为 0,新优先级为 -5
[root@bj-1-141 ~]# ps axo pid,comm,ni,priority|grep htop
 37810 htop             -5  15

20.18、网络客户端工具

20.18.1、ping命令

使用格式

ping [option] dest_host

常用选项

-c #:指定发送ping包的个数;
-I IFACE:指定从哪个接口发送;
-w #:ping命令超时时长;
-W #:一次ping操作中等待对方响应的超时时长;
-s #:指定一次ping包的大小;

示例

[root@bj-1-141 ~]# ping -c 10 192.168.1.161
PING 192.168.1.161 (192.168.1.161) 56(84) bytes of data.
64 bytes from 192.168.1.161: icmp_seq=1 ttl=64 time=0.490 ms
64 bytes from 192.168.1.161: icmp_seq=2 ttl=64 time=1.16 ms
64 bytes from 192.168.1.161: icmp_seq=3 ttl=64 time=0.635 ms
64 bytes from 192.168.1.161: icmp_seq=4 ttl=64 time=0.641 ms
64 bytes from 192.168.1.161: icmp_seq=5 ttl=64 time=0.607 ms
64 bytes from 192.168.1.161: icmp_seq=6 ttl=64 time=1.56 ms
64 bytes from 192.168.1.161: icmp_seq=7 ttl=64 time=1.54 ms
64 bytes from 192.168.1.161: icmp_seq=8 ttl=64 time=0.567 ms
64 bytes from 192.168.1.161: icmp_seq=9 ttl=64 time=0.558 ms
64 bytes from 192.168.1.161: icmp_seq=10 ttl=64 time=0.561 ms

--- 192.168.1.161 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9013ms
rtt min/avg/max/mdev = 0.490/0.832/1.561/0.401 ms

20.18.2、traceroute命令

功用:跟踪源主机到目标主机之间经过的网关;

使用格式

traceroute host

20.18.3、lftp命令

使用格式

lftp [-p port] [-u user [,pass]] [site] 

常用下载上传命令

get, mget, put, mput, rm ,mrm

20.18.4、lftpget命令

使用格式

lftpget [-c] [-d] [-v] URL [URL...]
-c:继续此前的下载;

20.18.5、wget命令

使用格式

wget [option]... [URL]...

常用选项

-b:在后台执行下载操作;
-q:静默模式下载,不现实下载进度;
-O file:保存下载的文件的保存位置;
-c:断点续传;
--limit-rate=amount:以指定的速率传输文件;

20.19、bash脚本编程之while循环

语法格式

while CONDITION;do
	循环体
	循环控制变量修正表达式
done

进入循环条件:CONDITION测试为“真”;

退出循环条件:CONDITION测试为“假”;

示例

练习1、求100以内所有正整数之和;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: sum5.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i sum=0
declare -i i=1

while [ $i -le 100 ];do
    let sum+=$i
    let i++
done

echo "$sum"

以上脚本的执行结果为:

[root@bj-1-141 scripts]# bash sum5.sh
5050

20.20、bash脚本编程之until循环

语法格式

until CONDITION;do
	循环体
	循环控制变量修正表达式
done

进入循环条件:CONDITION测试为“假”;

退出循环条件:CONDITION测试为“真”;

示例

练习:使用until循环实现100以内整数之和;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: sum6.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i sum=0
declare -i i=1

until [ $i -gt 100 ];do
    let sum+=$i
    let i++
done
echo "$sum"

以上脚本的执行结果为:

[root@bj-1-141 scripts]# bash sum6.sh
5050

20.21、练习部分

分别使用for, while,until实现

20.21.1、练习1、求100以内所有偶数之和;100以内奇数之和;

# while循环实现方式
#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: even_while.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i even_sum=0
declare -i odd_sum=0
declare -i i=1

while [ $i -le 100 ];do
    if [ $[ $i%2 ] -eq 0 ];then
        let even_sum+=$i
    else
        let odd_sum+=$i
    fi
    let i++
done

echo "evennumber_sum=$even_sum"
echo
echo "oddnumber_sum=$odd_sum"

# for循环方式实现
#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: even_for.sh
# Author: buhui
# Date: 2017-01-03
# Description:
declare -i even_sum=0
declare -i odd_sum=0

for num in {1..100};do
    if [[ $[ $num%2 ] -eq 0 ]];then
        let even_sum+=$num
    else
        let odd_sum+=$num
    fi
done

echo "even_number_sum=$even_sum"
echo "odd_number_sum=$odd_sum

20.21.2、练习创建10个用户,user101-user110;密码同用户名;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: adduser_while.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i num=101

while [ $num -lt 111 ];do
    if id user${num} &>/dev/null;then
        echo "user${num} is exsist."
    else
        useradd user$[num]
        echo "user${num}"|passwd --stdin user${num} &>/dev/null
        echo "add user user${num} finishd."
    fi
    let num++
done
[root@bj-1-141 scripts]# bash adduser_while.sh
add user user101 finishd.
add user user102 finishd.
add user user103 finishd.
add user user104 finishd.
add user user105 finishd.
add user user106 finishd.
add user user107 finishd.
add user user108 finishd.
add user user109 finishd.
add user user110 finishd.
[root@bj-1-141 scripts]# tail /etc/passwd
user101:x:5003:5007::/home/user101:/bin/bash
user102:x:5004:5008::/home/user102:/bin/bash
user103:x:5005:5009::/home/user103:/bin/bash
user104:x:5006:5010::/home/user104:/bin/bash
user105:x:5007:5011::/home/user105:/bin/bash
user106:x:5008:5012::/home/user106:/bin/bash
user107:x:5009:5013::/home/user107:/bin/bash
user108:x:5010:5014::/home/user108:/bin/bash
user109:x:5011:5015::/home/user109:/bin/bash
user110:x:5012:5016::/home/user110:/bin/bash

20.21.3、打印九九乘法表

for循环方式实现

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: for992.sh
# Author: buhui
# Date: 2017-01-04
# Description:
for j in {1..9};do
    for i in $(seq 1 $j);do
        echo -n -e "${i}X${j}=$[ $i*$j ]\t"
    done
    echo
done
# 以上脚本执行结果为
[root@bj-1-141 scripts]# bash for992.sh
1X1=1
1X2=2	2X2=4
1X3=3	2X3=6	3X3=9
1X4=4	2X4=8	3X4=12	4X4=16
1X5=5	2X5=10	3X5=15	4X5=20	5X5=25
1X6=6	2X6=12	3X6=18	4X6=24	5X6=30	6X6=36
1X7=7	2X7=14	3X7=21	4X7=28	5X7=35	6X7=42	7X7=49
1X8=8	2X8=16	3X8=24	4X8=32	5X8=40	6X8=48	7X8=56	8X8=64
1X9=9	2X9=18	3X9=27	4X9=36	5X9=45	6X9=54	7X9=63	8X9=72	9X9=81

while循环方式实现

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: while992.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i j=1

while [ $j -le 9 ];do
    declare -i i=1
    while [ $i -le $j ];do
        echo -n -e "${i}X${j}=$[ $i*$j ]\t"
        let i++
    done
    let j++
    echo
done

# 以上脚本执行结果为
[root@bj-1-141 scripts]# bash while992.sh
1X1=1
1X2=2	2X2=4
1X3=3	2X3=6	3X3=9
1X4=4	2X4=8	3X4=12	4X4=16
1X5=5	2X5=10	3X5=15	4X5=20	5X5=25
1X6=6	2X6=12	3X6=18	4X6=24	5X6=30	6X6=36
1X7=7	2X7=14	3X7=21	4X7=28	5X7=35	6X7=42	7X7=49
1X8=8	2X8=16	3X8=24	4X8=32	5X8=40	6X8=48	7X8=56	8X8=64
1X9=9	2X9=18	3X9=27	4X9=36	5X9=45	6X9=54	7X9=63	8X9=72	9X9=81

20.21.4、打印逆序九九乘法表

for循环方式实现

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: reverse_for.sh
# Author: buhui
# Date: 2017-01-04
# Description:
for j in {9..1};do
    for i in $(seq 1 $j);do
        echo -n -e "${i}X${j}=$[ $i*$j ]\t"
    done
    echo
done

# 以上脚本的执行结果为
[root@bj-1-141 scripts]# bash reverse_for.sh
1X9=9	2X9=18	3X9=27	4X9=36	5X9=45	6X9=54	7X9=63	8X9=72	9X9=81
1X8=8	2X8=16	3X8=24	4X8=32	5X8=40	6X8=48	7X8=56	8X8=64
1X7=7	2X7=14	3X7=21	4X7=28	5X7=35	6X7=42	7X7=49
1X6=6	2X6=12	3X6=18	4X6=24	5X6=30	6X6=36
1X5=5	2X5=10	3X5=15	4X5=20	5X5=25
1X4=4	2X4=8	3X4=12	4X4=16
1X3=3	2X3=6	3X3=9
1X2=2	2X2=4
1X1=1

while循环方式实现

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: reverse_while.sh
# Author: buhui
# Date: 2017-01-04
# Description:
declare -i j=9

while [ $j -le 9 -a $j -ge 1 ];do
    declare -i i=1
    while [ $i -le $j ];do
        echo -n -e "${i}X${j}=$[ $i*$j ]\t"
        let i++
    done
    let j--
    echo
done

# 以上脚本的执行结果为
[root@bj-1-141 scripts]# bash reverse_while.sh
1X9=9	2X9=18	3X9=27	4X9=36	5X9=45	6X9=54	7X9=63	8X9=72	9X9=81
1X8=8	2X8=16	3X8=24	4X8=32	5X8=40	6X8=48	7X8=56	8X8=64
1X7=7	2X7=14	3X7=21	4X7=28	5X7=35	6X7=42	7X7=49
1X6=6	2X6=12	3X6=18	4X6=24	5X6=30	6X6=36
1X5=5	2X5=10	3X5=15	4X5=20	5X5=25
1X4=4	2X4=8	3X4=12	4X4=16
1X3=3	2X3=6	3X3=9
1X2=2	2X2=4
1X1=1

20.21.5、九九乘法表过程剖析

外层循环控制乘数,内循环控制被乘数;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: for992.sh
# Author: buhui
# Date: 2017-01-04
# Description:
for j in {1..9};do
    for i in $(seq 1 $j);do
        echo -n -e "${i}X${j}=$[ $i*$j ]\t"
    done
    echo
done
外层循环第一遍j=1,进入内层循环,$(seq 1 $j)此时$j=1,生成列表中只有一个数字1,所以i=1;那么$i*$j就等于1X1=1;一次循环结束;
	外层循环第二遍j=2,进入内层循环,$(seq 1 $j)此时$j=2,生成列表中有2个数字1,2,所以内存循环会循环两次,第一次i=1;那么$i*$j就等于1X2=2;第二次i=2;那么那么$i*$j就等于2X2=4;
	以此类推...

while循环也是类似方式实现;

posted @ 2017-01-04 14:50  EnZhiWang  阅读(3080)  评论(0编辑  收藏  举报