天天看點

Linux 學習基礎之系統管理——工作管理

1、把程序放入背景

》tar -zcf  etc.tar.gz  /etc &

#該方法把tar 程序放入背景時,tar程序并未停止,還在背景運作

[[email protected] ~]# tar -zcf testfile /etc &
[1] 3066
[[email protected] ~]# tar: 從成員名中删除開頭的“/”

[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# tar -zcf testfile /etc &tar: 從硬連接配接目标中删除開頭的“/”
                    
[1]+  Done                    tar -zcf testfile /etc
           

》按下Ctrl+z 的方式将某個程序放入背景,比如把top程序放入背景

[[email protected] ~]# top

#在top 指令執行的過程中,按下Ctrl+z快捷鍵,但該方法放入背景的程序會終止運作

注意:

Windows中,把視窗縮小,就是把程序放入背景;

Linux圖形界面中,和Windows一樣,但是指令行界面,通過

在某個指令後加& 符合,即可把該程序放入背景,或者通過執行top指令時,按下Ctrl+z 實作

2、檢視背景的工作

[[email protected] ~]# jobs [-l ]

選項:

   -l  :顯示工作的PID

注:  +  号代表最近一個放入背景的工作,也是工作恢複時,預設恢複的工作

           -   号代表倒數第二個放入背景的工作

[[email protected] ~]# tar -zcf testfile /etc &
[1] 3114
[[email protected] ~]# tar: 從成員名中删除開頭的“/”

[[email protected] ~]# 
[[email protected] ~]# tar -zcf testfile /etc &tar: 從硬連接配接目标中删除開頭的“/”
                    jobs -l
[1]+  3114 Done                    tar -zcf testfile /etc
           
[[email protected] ~]# top
top - 01:10:31 up 57 min,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 171 total,   1 running, 170 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.5%us,  1.8%sy,  0.0%ni, 95.4%id,  0.8%wa,  0.1%hi,  0.4%si,  0.0%st
Mem:   1906188k total,   912216k used,   993972k free,    30624k buffers
Swap:  2097148k total,        0k used,  2097148k free,   521028k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                            
  3129 root      20   0 15024 1164  832 R  2.9  0.1   0:00.21 top                                 
     1 root      20   0 19364 1568 1240 S  0.0  0.1   0:03.52 init                                
     2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd                            
     3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                         
     4 root      20   0     0    0    0 S  0.0  0.0   0:00.14 ksoftirqd/0                         
     5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0                           
     6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                          
     7 root      20   0     0    0    0 S  0.0  0.0   0:07.77 events/0                            
     8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events/0                            
     9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_long/0                       
    10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_power_ef                     
    11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup                              
    12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper                             
    13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns                               
    14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr                           
    15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                                  
    16 root      20   0     0    0    0 S  0.0  0.0   0:00.18 sync_supers                         
    17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default                         
    18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                       
    19 root      20   0     0    0    0 S  0.0  0.0   0:00.89 kblockd/0                           
    20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid                              
    21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                        
    22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug                       
    23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux                             
    24 root      20   0     0    0    0 S  0.0  0.0   0:00.21 ata_sff/0                           
    25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd                       

[1]+  Stopped                 top
[[email protected] ~]# 
[[email protected] ~]# top
top - 01:10:39 up 57 min,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 172 total,   1 running, 170 sleeping,   1 stopped,   0 zombie
Cpu(s):  1.5%us,  1.8%sy,  0.0%ni, 95.4%id,  0.8%wa,  0.1%hi,  0.4%si,  0.0%st
Mem:   1906188k total,   912644k used,   993544k free,    30624k buffers
Swap:  2097148k total,        0k used,  2097148k free,   521028k cached

   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                            
   887 root      20   0     0    0    0 S  2.0  0.0   0:00.68 vmmemctl                            
  3130 root      20   0 15024 1172  832 R  2.0  0.1   0:00.01 top                                 
     1 root      20   0 19364 1568 1240 S  0.0  0.1   0:03.52 init                                
     2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd                            
     3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                         
     4 root      20   0     0    0    0 S  0.0  0.0   0:00.14 ksoftirqd/0                         
     5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0                           
     6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                          
     7 root      20   0     0    0    0 S  0.0  0.0   0:07.77 events/0                            
     8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events/0                            
     9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_long/0                       
    10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_power_ef                     
    11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup                              
    12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper                             
    13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns                               
    14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr                           
    15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                                  
    16 root      20   0     0    0    0 S  0.0  0.0   0:00.18 sync_supers                         
    17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default                         
    18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                       
    19 root      20   0     0    0    0 S  0.0  0.0   0:00.89 kblockd/0                           
    20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid                              
    21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                        
    22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug                       
    23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux                             
    24 root      20   0     0    0    0 S  0.0  0.0   0:00.21 ata_sff/0                           

[2]+  Stopped                 top
[[email protected] ~]# top
top - 01:10:42 up 57 min,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 173 total,   1 running, 170 sleeping,   2 stopped,   0 zombie
Cpu(s):  1.5%us,  1.8%sy,  0.0%ni, 95.4%id,  0.8%wa,  0.1%hi,  0.4%si,  0.0%st
Mem:   1906188k total,   912948k used,   993240k free,    30624k buffers
Swap:  2097148k total,        0k used,  2097148k free,   521028k cached
 Unknown command - try 'h' for help 
   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                            
     7 root      20   0     0    0    0 S  1.9  0.0   0:07.78 events/0                            
  3131 root      20   0 15024 1164  832 R  1.9  0.1   0:00.02 top                                 
     1 root      20   0 19364 1568 1240 S  0.0  0.1   0:03.52 init                                
     2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd                            
     3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0                         
     4 root      20   0     0    0    0 S  0.0  0.0   0:00.15 ksoftirqd/0                         
     5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0                           
     6 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                          
     8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events/0                            
     9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_long/0                       
    10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 events_power_ef                     
    11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cgroup                              
    12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper                             
    13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns                               
    14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr                           
    15 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm                                  
    16 root      20   0     0    0    0 S  0.0  0.0   0:00.18 sync_supers                         
    17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default                         
    18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0                       
    19 root      20   0     0    0    0 S  0.0  0.0   0:00.89 kblockd/0                           
    20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid                              
    21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify                        
    22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug                       
    23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux                             
    24 root      20   0     0    0    0 S  0.0  0.0   0:00.21 ata_sff/0                           
    25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd                       

[3]+  Stopped                 top
[[email protected] ~]# jobs -l
[1]   3129 停止 (信号)         top
[2]-  3130 停止 (信号)         top
[3]+  3131 停止 (信号)         top
           

注:以上示例說明Ctrl+z 快捷鍵可以将正在運作的程序放入背景

      而且該方式放入 背景的程序會終止執行,與& 方式有所差別

      以上【1】表示目前背景程序的工作号,注意與pid差別

3、将背景暫停的工作恢複到前台執行

[[email protected] ~]# fg  %工作号

參數:

%工作号  :%号可以省略,但是注意工作号和PID的差別

4、将前台工作恢複到背景運作的方式

[[email protected] ~]# bg  %工作号

注意:top指令必須放在前台運作,背景恢複執行的指令,是不能和前台有互動的,

否則不能恢複到背景

和前台使用者有互動的指令,比如 top  vi等。這些指令放到背景毫無意義。

5、如何停止背景程序?

注意,Ctrl+c等停止運作的指令,隻對在前台的程序有用,

放在背景的程序是無法接受Ctrl+c 指令的,要想停止放在背景的程序,隻能

先用fg  調到前台,再使用Ctrl+c等其他指令

繼續閱讀