小白雲路-------linux基礎-進程

hello,world
hello, linux
hello, everyone
linux基礎小課堂持續更新~~~

進程管理
===================================

  ps  process snapshot

查看當前終端上運行的進程
[root@youryg ~]#  ps 
[root@youryg ~]#  ps aux
[root@youryg ~]#  ps auxf

a 只能查看所有終端進程
u 顯示進程擁有者
x 顯示系統內所有進程
f  顯示進程之間的父子關係(使用pstree查看更詳細的父子關係

[root@youryg ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 193628 5568 ? Ss 11:00 0:13 /usr/lib/system
root 2 0.0 0.0 0 0 ? S 11:00 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 11:00 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 11:00 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 11:00 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 11:00 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S 11:00 0:11 [rcu_sched]

user 進程擁有者
pid  process identify
TTY  進程在哪個終端運行
     查看tty的方法:
    [root@youryg ~]  # tty
     ? 表示這個進程開啓的時候沒有佔用終端

time 進程佔用cpu的總時間
cmd  進程名稱
%cpu 進程佔用的cpu百分比
%mem 進程佔用memory百分比
VSZ  進程佔用的虛擬內存大小
RSS  佔用的物理內存大小
STAT 當前進程狀態
     #man ps
     R running
     S sleep
     T stop  
     Z zombie(僵死,殭屍)

        D    Uninterruptible sleep (usually IO)
        R    Running or runnable (on run queue)
        S    Interruptible sleep (waiting for an event to complete)
        T    Stopped, either by a job control signal or because it is
             being traced.
        W    paging (not valid since the 2.6.xx kernel)
        X    dead (should never be seen)
        Z    Defunct ("zombie") process, terminated but not reaped by
            its parent.
        <    high-priority (not nice to other users)
        N    low-priority (nice to other users)
        L    has pages locked into memory (for real-time and
            custom IO)
        s    is a session leader
        l    is multi-threaded (using CLONE_THREAD, like NPTL
                pthreads do)
        +    is in the foreground process group

按指定字段排序:

[root@youryg ~]# ps aux --sort %cpu | less
[root@youryg ~]# ps aux --sort %-cpu | less

顯示指定字段:

[root@youryg ~]# ps axo user,%cpu,command --sort -%cpu | less
[root@youryg ~]# ps -eo user,%cpu,%mem,command --sort %cpu

[root@youryg ~]# ps -elf
-e  顯示所有進程    
-l  長格式顯示
-f  完整格式    
[root@youryg ~]# ps -e 常用

pstree
查看進程樹
#pstree
-a 顯示參數
-p 顯示pid
-u 顯示用戶名

lsof(應用範圍有限,只能查看帶端口的進程)
lsof -i:80
查看端口爲80的進程

pidof
#pidof 進程名稱

pgrep

pgrep -l eyes

1179 xeyes

進程pid文件
#cd /var/run
[root@youryg ~]# cat /var/run/httpd/httpd.pid
12265

======================================
w
[root@server ~]# w
16:51:43 up 8:30, 7 users, load average: 0.11, 0.27, 0.30
USER TTY FROM LOGIN@ IDLE JCPU
PCPU WHAT
root :0 :0 08:22 ?xdm? 29:39 0.23s
gdm-session-
root pts/0 :0 14:13 1:43 0.30s 0.30s bash
root pts/1 :0 11:19 5:19 0.62s 0.62s bash
root pts/2 :0 14:13 1:39m 0.06s 0.01s less
root pts/3 :0 14:54 7.00s 0.06s 0.01s w
root pts/4 :0 16:25 25:51 0.06s 0.06s bash
root pts/5 :0 16:40 7:11 0.24s 0.24s bash

======================================

who
[root@server ~]# who
root :0 2017-11-27 08:22 (:0)
root pts/0 2017-11-27 14:13 (:0)
root pts/1 2017-11-27 11:19 (:0)
root pts/2 2017-11-27 14:13 (:0)
root pts/3 2017-11-27 14:54 (:0)
root pts/4 2017-11-27 16:25 (:0)
root pts/5 2017-11-27 16:40 (:0)

======================================

users
[root@server ~]# users
root root root root root root root

======================================

top

實時的查看進程的狀態
h|? 幫助

往下翻頁
< 往上翻頁
M 按內存排序
P 按cpu排序
k 輸入pid殺死進程
h 打印幫助
q 退出

1 
f
W

R  排序反轉   
z  彩色顯示
r   調整進程的nice優先級

[root@youryg ~]# top -d 2 -p 1068 -bn1
[root@youryg ~]# top -u apache

=================================================
進程控制
按pid殺死進程
#kill 信號(signal) pid
-1 HUP 重新加載進程或者重新加載配置文件
-9 KILL 強制殺死
-15 TERM 正常殺死(這個信號可以默認不寫)
-18 CONT 激活進程
-19 STOP 掛起進程
#kill -HUP pid
#kill -STOP pid

#kill -l
#man 7 signal 所有信號的解釋

#killall 信號 進程名稱

#pkill -9 進程名稱
#pkill -t 終端  
不加-9只殺死在終端上運行的進程,加-9連終端本身一起幹掉
#pkill -u 用戶名稱
#xkill

======================================
作業控制:
作業控制之jobs:
ctrl+z 把程序放到後臺(這方法會讓程序在後臺暫停)
#fg %1 把程序調到前臺,%是用來修飾job number,1就是job number。
#jobs 查看工作號
#bg %1 讓暫停的程序在後臺運行
#kill -9 %1
#kill -9 pid

======================================
進程優先級

優先級本身是"不能"修改,通過nice值修改優先級。
普通賬戶只能調高nice值,不能往低調
root賬戶隨便調
nice值越高,優先級越低。
nice值範圍:-20到19

查看進程優先級
    # top
    # ps axo pid,command,nice  --sort=-nice | less
    # ps -elf
    PR:priority
    NI:nice

指定進程優先級
    #nice --15 firefox &   
修改進程優先級
    # renice 10 7116   nice值前面不能加-
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章