一個進程可以包含多個線程且至少有一個主線程,同一進程的線程共享該進程的代碼和數據
對於Linux進程的管理,是通過進程管理工具實現的,比如ps、kill
命令:ps
作用:查看系統進程( 靜態查看 )
語法:ps [選項]
我們常用的選項是組合是aux
查看隸屬於自己的進程
[root@VM_200_13_CentOS ~]# ps -u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1275 0.0 0.0 6452 804 ttyS0 Ss+ Aug15 0:00 /sbin/agetty --keep-baud 115200 3
root 1276 0.0 0.0 110032 856 tty1 Ss+ Aug15 0:00 /sbin/agetty --noclear tty1 linux
root 2828 0.0 0.2 12684 2888 pts/0 Ss 14:33 0:00 -bash
root 2979 0.0 0.1 35888 1476 pts/0 R+ 14:34 0:00 ps -u
查看所有用戶的進程和沒有控制終端的進程
[root@VM_200_13_centos ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 44800 7272 ? Ss Aug15 0:22 /usr/lib/systemd/systemd --switch
root 2 0.0 0.0 0 0 ? S Aug15 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Aug15 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Aug15 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Aug15 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Aug15 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S Aug15 0:00 [rcuob/0]
root 10 0.0 0.0 0 0 ? R Aug15 0:03 [rcu_sched]
root 11 0.0 0.0 0 0 ? S Aug15 0:06 [rcuos/0]
root 12 0.0 0.0 0 0 ? S Aug15 0:00 [watchdog/0]
root 13 0.0 0.0 0 0 ? S< Aug15 0:00 [khelper]
...
ps -aux輸出解釋:
進程狀態:
查看nginx進程
[root@VM_200_13_centos ~]# ps -aux | grep nginx
root 5092 0.0 0.0 9036 672 pts/0 R+ 15:07 0:00 grep --color=auto nginx
root 17698 0.0 0.0 20468 604 ? Ss Aug15 0:00 nginx: master process nginx
nginx 17699 0.0 0.1 22980 1612 ? S Aug15 0:00 nginx: worker process
命令:top
作用:動態 顯示當前系統正在執行的進程的相關信息,包括進程ID、內存占用率、CPU占用率等
語法:top [選項]
說明:默認3s刷新一次,按空格立即刷新,按q退出,按大寫M則根據內存排序,按P則根據CPU排序
top - 15:58:43 up 2 days, 14:09, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 77 total, 3 running, 74 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016916 total, 533700 free, 90484 used, 392732 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 768064 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 44900 7360 2364 S 0.0 0.7 0:23.01 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.56 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
10 root 20 0 0 0 0 S 0.0 0.0 0:03.61 rcu_sched
11 root 20 0 0 0 0 R 0.0 0.0 0:06.97 rcuos/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.14 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
26 root 20 0 0 0 0 S 0.0 0.0 0:00.03 khungtaskd
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
28 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
29 root 39 19 0 0 0 S 0.0 0.0 0:00.56 khugepaged
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_mark
31 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
39 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
41 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kmpath_rdacd
42 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
43 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ipv6_addrconf
63 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq
101 root 20 0 0 0 0 S 0.0 0.0 0:00.18 kauditd
213 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
前五行是當前系統情況整體的統計信息區,下面我們看每一行信息的具體意義
第一行:任務隊列信息
第二行:進程信息
第三行:CPU信息(如果有多個CPU的話信息會超過兩行)
第四行:內存信息
第五行:交換區信息
剩下的則是進程信息:
[root@VM_200_13_centos ~]# pgrep ssh
697
8435
命令:pstree
作用:以樹狀圖顯示進程,只顯示進程的名字,且相同進程合並顯示
語法:pstree [選項]
[root@VM_200_13_centos ~]# pstree -p
systemd(1)-+-acpid(474)
|-agetty(1275)
|-agetty(1276)
|-atd(472)
|-auditd(430)---{auditd}(448)
|-barad_agent(1938)-+-barad_agent(1939)
| `-barad_agent(1940)-+-{barad_agent}(1941)
| `-{barad_agent}(2009)
|-chronyd(477)
|-crond(473)
|-dbus-daemon(454)
|-lsmd(452)
|-lvmetad(357)
|-nginx(17698)---nginx(17699)
|-rsyslogd(469)-+-{rsyslogd}(485)
| `-{rsyslogd}(486)
|-sap1002(1853)
|-sap1007(1861)
|-secu-tcs-agent(1153)---sap1004(1859)
|-sgagent(1922)---{sgagent}(1923)
|-sshd(697)---sshd(8435)---bash(8437)---pstree(11096)
|-systemd-journal(329)
|-systemd-logind(458)
|-systemd-udevd(359)
`-tuned(695)-+-{tuned}(1329)
|-{tuned}(1330)
|-{tuned}(1332)
`-{tuned}(1335)
介紹netstat工具
命令:netstat
作用:顯示各種網絡相關信息,如網絡連接,路由表,接口狀態
語法:netstat [選項]
查看所有的TCP端口
[root@VM_200_13_centos ~]# netstat -anlpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17698/nginx: mast
er
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 697/sshd
tcp 0 0 10.104.200.13:80 115.221.11.116:2137 ESTABLISHED 17699/nginx: work
er
tcp 0 0 10.104.200.13:80 115.221.14.249:4282 ESTABLISHED 17699/nginx: work
er
tcp 0 0 10.104.200.13:80 115.221.11.116:3583 ESTABLISHED 17699/nginx: work
er
tcp 0 52 10.104.200.13:22 182.150.21.5:6390 ESTABLISHED 8435/sshd: root@p
ts
tcp 0 0 10.104.200.13:80 115.221.14.139:4747 FIN_WAIT2 -
tcp 0 0 10.104.200.13:80 115.221.11.116:3797 ESTABLISHED 17699/nginx: work
er
tcp 0 0 10.104.200.13:80 115.221.11.116:3276 FIN_WAIT2 -
tcp 0 0 10.104.200.13:59586 10.249.80.72:9988 ESTABLISHED 1153/secu-tcs-age
nt
tcp 0 0 10.104.200.13:80 115.221.12.48:1670 FIN_WAIT2 -
tcp6 0 0 :::22 :::* LISTEN 697/sshd
查看某個服務的端口
[root@VM_200_13_centos ~]# netstat -anlp | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 697/sshd
tcp 0 52 10.104.200.13:22 182.150.21.5:6390 ESTABLISHED 8435/sshd: root@pts
tcp6 0 0 :::22 :::* LISTEN 697/sshd
unix 3 [ ] STREAM CONNECTED 12545 697/sshd
unix 2 [ ] DGRAM 1401521 8435/sshd: root@pts
查看所有的UDP端口
[root@VM_200_13_centos ~]# netstat -anlpu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 127.0.0.1:323 0.0.0.0:* 477/chronyd
udp6 0 0 ::1:323 :::* 477/chronyd
查看nginx
[root@VM_200_13_centos ~]# netstat -anlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17698/nginx: master
tcp 0 0 10.104.200.13:80 115.221.11.116:4493 ESTABLISHED 17699/nginx: worker
tcp 0 0 10.104.200.13:80 115.221.12.48:2473 ESTABLISHED 17699/nginx: worker
unix 3 [ ] STREAM CONNECTED 488644 17698/nginx: master
unix 3 [ ] STREAM CONNECTED 488643 17698/nginx: master
命令:kill
作用:通過信號的方式來控制進程
語法:kill [選項] [進程號]
查看所有的進程信號
[root@VM_200_13_centos ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
關閉進程
[root@VM_200_13_centos ~]# vim test.txt
[1]+ Stopped vim test.txt
[root@VM_200_13_centos ~]# ps -aux | grep vim
root 14797 0.0 0.4 49768 4848 pts/0 T 17:38 0:00 vim test.txt
root 14859 0.0 0.0 9036 668 pts/0 R+ 17:39 0:00 grep --color=auto vim
[root@VM_200_13_centos ~]# kill -9 14797
[root@VM_200_13_centos ~]# ps -aux | grep vim
root 14870 0.0 0.0 9036 672 pts/0 R+ 17:39 0:00 grep --color=auto vim
[1]+ Killed vim test.txt
重啟進程
[root@VM_200_13_centos ~]# ps -aux | grep nginx
root 8582 0.0 0.0 9036 672 pts/0 R+ 00:40 0:00 grep --color=auto nginx
root 17698 0.0 0.0 20468 604 ? Ss Aug15 0:00 nginx: master process nginx
nginx 17699 0.0 0.1 22980 1612 ? S Aug15 0:01 nginx: worker process
[root@VM_200_13_centos ~]# kill -1 17698
linux系統中的killall命令用於殺死指定名字的進程(kill processes by name)
我們可以使用kill命令殺死指定進程PID的進程,如果要找到我們需要殺死的進程,我們還需要在之前使用ps等命令再配合grep來查找進程
而killall把這兩個過程合二為一,是一個很好用的命令
命令:killall
作用:用來結束同名的的所有進程
語法:killall [選項] 進程名稱
關閉所有httpd進程[root@VM_200_13_centos ~]# yum -y install httpd [root@VM_200_13_centos ~]# systemctl start httpd [root@VM_200_13_centos3 ~]# ps -aux | grep httpd root 39962 0.0 0.1 226128 5052 ? Ss 08:38 0:00 /usr/sbin/httpd -DFOREGROUND apache 39974 0.0 0.0 228212 3128 ? S 08:39 0:00 /usr/sbin/httpd -DFOREGROUND apache 39975 0.0 0.0 228212 3128 ? S 08:39 0:00 /usr/sbin/httpd -DFOREGROUND apache 39976 0.0 0.0 228212 3128 ? S 08:39 0:00 /usr/sbin/httpd -DFOREGROUND apache 39977 0.0 0.0 228212 3128 ? S 08:39 0:00 /usr/sbin/httpd -DFOREGROUND apache 39978 0.0 0.0 228212 3128 ? S 08:39 0:00 /usr/sbin/httpd -DFOREGROUND root 39985 0.0 0.0 112644 956 pts/1 S+ 08:39 0:00 grep --color=auto httpd [root@VM_200_13_centos3 ~]# killall httpd
Linux一切皆文件,設備(文件)可以通過讀寫來操作
/proc是內存中有關 系統進程 的信息
/sys是有關 系統內核以及驅動 的信息
查看當前CPU信息
[root@VM_200_13_centos ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 63
model name : Intel(R) Xeon(R) CPU E5-26xx v3
stepping : 2
microcode : 0x1
cpu MHz : 2294.686
cache size : 4096 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall
nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer
aes xsave avx f16c rdrand hypervisor lahf_lm abm xsaveopt
bogomips : 4589.37
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:
開啟內核轉發功能
[root@VM_200_13_centos ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@VM_200_13_centos ~]# cat /proc/sys/net/ipv4/ip_forward
1
後台進程:
也叫守護進程(Daemon),是運行在後台的一種特殊進程,守護的意思就是不受終端控制。Linux的大多數服務器就是用守護進程實現的比如,Web服務器httpd等
前台進程:
用戶使用的有控制終端的進程
跟系統任務相關的幾個命令:fg、bg、jobs、&、ctrl+z
| Header One | Header Two |
| :------------- | :------------- |
| & | 用在一個命令的最後,可以把這個命令放到後台執行 |
| ctrl+z | 將一個正在前台執行的命令放到後台,並且暫停 |
| jobs | 查看當前有多少在後台運行的進程 |
| fg | 將後台中的命令調至前台繼續運行
如果後台中有多個命令,可以用 fg %jobnumber將選中的命令調出
%jobnumber是通過jobs命令查到的後台正在執行的命令的序號(不是pid) |
| bg | 將一個在後台暫停的命令,變成繼續執行
如果後台中有多個命令,可以用bg %jobnumber將選中的命令調出
%jobnumber是通過jobs命令查到的後台正在執行的命令的序號(不是pid) |
恢復被掛起的進程
[root@VM_200_13_centos ~]# vim test.txt
[1]+ Stopped vim test.txt
[root@VM_200_13_centos ~]# ps -aux |grep vim
root 13516 0.1 0.4 49756 4804 pts/0 T 17:18 0:00 vim test.txt
root 13518 0.0 0.0 9036 672 pts/0 R+ 17:18 0:00 grep --color=auto vim
[root@VM_200_13_centos ~]# jobs
[1]+ Stopped vim test.txt
[root@VM_200_13_centos ~]# fg %1
vim test.txt
優先級取值范圍為[-20,19],越小優先級越高, 默認優先級是0
命令:nice
作用:設置進程的優先級
語法:nice [選項] 命令
說明:nice的選項只有一個-n,參數後面跟上[-20,19]范圍內的值,不使用該參數則以10為值設置nice值
[root@VM_200_13_centos ~]# nice vim v.txt
[1]+ Stopped nice vim v.txt
[root@VM_200_13_centos ~]# ps -aux | grep vim
root 10029 0.0 0.4 49748 4800 pts/0 TN 01:01 0:00 vim v.txt
root 10038 0.0 0.0 9036 672 pts/0 R+ 01:01 0:00 grep --color=auto vim
使用top命令查看上面操作的nice值
[root@VM_200_13_centos ~]# top -n 1 -p 10029
top - 01:03:25 up 2 days, 23:13, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 1 total, 0 running, 0 sleeping, 1 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.5 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016916 total, 511816 free, 93860 used, 411240 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 763804 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10029 root 30 10 49748 4800 2484 T 0.0 0.5 0:00.02 vim
命令:renice
作用:改變一個正在運行的進程的優先級
語法:renice [-n] 優先級值 [-gpu] identifier..
[root@VM_200_13_centos ~]# ps -aux | grep vim
root 10029 0.0 0.4 49748 4800 pts/0 TN 01:01 0:00 vim v.txt
root 10909 0.0 0.0 9036 668 pts/0 R+ 01:15 0:00 grep --color=auto vim
[root@VM_200_13_centos ~]# renice -n -20 10029
10029 (process ID) old priority 10, new priority -20
Screen是Linux下的一款遠程會話管理工具,可以在多個進程之間多路復用一個物理終端的全屏窗口管理器
它可以創建多個會話(Session),每個會話中可以創建多個窗口(Window)
每個窗口中可以運行單獨的任務,並且互相之間不受影響,還可以方便快速的在不同的窗口和會話之間切換
Screen可以看作是窗口管理器的命令行界面版本。它提供了統一的管理多個會話的界面和相應的功能
會話共享
Screen可以讓一個或多個用戶從不同終端多次登錄一個會話,並共享會話的所有特性(比如可以看到完全相同的輸出)
它同時提供了窗口訪問權限的機制,可以對窗口進行密碼保護
在VPS中執行一些非常耗時的任務時(如下載,壓縮,解壓縮,編譯,安裝等)
我們通常是單獨開一個遠程終端窗口來執行這個任務,且在任務執行過程中不能關閉這個窗口或者中斷連接,否則正在執行的任務會被終止掉
而有了screen,我們可以在一個窗口中安裝程序,然後在另一個窗口中下載文件,再在第三個窗口中編譯程序
只需要一個SSH連接就可以同時執行這三個任務,還可以方便的在不同會話或窗口中切換
即使因為意外導致窗口關閉或者連接中斷,也不會影響這三個任務的執行
命令:screen
作用:可以在多個進程之間多路復用一個物理終端的全屏窗口管理器
語法:screen [選項] [命令]
實際使用
使用Xshell連接服務器,進入screen
[root@VM_200_13_centos ~]# screen
在screen窗口執行updatedb模擬執行一個耗時很長的操作
在screen窗口鍵入Ctrl+a+d模擬中斷,Screen會給出detached提示
[detached from 13137.pts-0.VM_200_13_centos]
退出Xshell或重連,再查看所有的screen會話
[root@VM_200_13_centos ~]# screen -ls
There is a screen on:
13137.pts-0.VM_200_13_centos (Detached)
1 Socket in /var/run/screen/S-root.
重連之前的screen
[root@VM_200_13_centos ~]# screen -r 1