目錄
- 進程的相關概念
- 進程查看及管理工具的使用
- Linux系統作業控制
- 調整進程優先級
- 網絡客戶端工具
- bash之while循環
20.1、進程類型
守護進程
daemon,在系統引導過程中啟動的進程;跟終端無關的進程;
前台進程
跟終端相關,通過終端啟動的進程;也可以把前台啟動的進程送給后台,以守護模式運行;
20.2、進程狀態
-
運行態:running
-
就緒態:ready
-
睡眠態:
可中斷:interruptable
不可中斷:uninterruptible
-
停止態:暫停於內存中,但不會被調度執行,除非手動啟動;stopped;
-
僵死態: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...
信號的標識方法
- 信號的數字標識
- 信號的完整名稱
- 信號的簡寫名稱( 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循環也是類似方式實現;