8.6 管道符和做業控制

管道符、做業控制

  • ctrl z //暫停一個任務
  • jobs //查看後臺的任務
  • bg [id] //把任務調到後臺
  • fg [id] //把任務調到前臺
  • 命令後面加&直接丟到後臺

管道符的使用

  • 管道符 | ,表示把前面命令輸出的結果,傳輸給後面的命令
  • cat 1.txt |wc -l ;cat 1.txt |grep 'aaa'
    • grep 命令,用來過濾指定關鍵詞的命令,只要在一行中含有這個關鍵詞,就會把這一行過濾出來
    • wc -l 命令,查看文件有多少個
[root@localhost ~]# ls
111  123  1.txt  234  2.txt  2.txt.bak  3.txt  anaconda-ks.cfg
[root@localhost ~]# ls | wc -l    
8
  • find ./ -type f //在當前目錄下,列出全部的文件
    • find ./ -type f |wc -l //計算當前目錄下有多少個文件
[root@localhost ~]# find ./ -type f
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.viminfo
./1.txt
./2.txt
./3.txt
./2.txt.bak
[root@localhost ~]# find ./ -type f |wc -l    計算當前目錄下,有多少個文件
12

做業控制

  • ctrl+z快捷鍵,暫停一個任務
    • 如果正在編輯一個文件的時候
      • 能夠ctrl+z臨時暫停下這個服務(丟到後臺去了),回到命令行界面,去操做其餘的任務
      • fg 命令能夠把丟在後臺的命令,調回前臺
    • 能夠控制多個任務,將他們暫停掉
  • jobs 命令,能夠把暫停的任務列出來
    • 暫停多個任務後,並會顯示中止的任務列出來
[root@localhost ~]# vim 1.txt

[1]+  已中止               vim 1.txt
[root@localhost ~]# fg
vim 1.txt

[1]+  已中止               vim 1.txt
[root@localhost ~]# jobs
[1]+  已中止               vim 1.txt
[root@localhost ~]# vim 2.txt

[2]+  已中止               vim 2.txt
[root@localhost ~]# jobs
[1]-  已中止               vim 1.txt
[2]+  已中止               vim 2.txt
[root@localhost ~]#
  • fg [id] 命令,把任務調到前臺並執行——>不加id號就是執行最後一次的任務(加id就是指定任務)
    • 能夠選擇執行的任務
[root@localhost ~]# fg 1
  • bg [id] 命令,把任務調到後臺並執行
[root@localhost ~]# bg 1
[1]+ vim 1.txt &

運行一條命令,能夠將它丟到後臺(前臺)去運行 在結束任務的時候,必須是在前臺才能結束——>(不然在後臺是沒法結束任務的)vim

  • sleep 1000 命令,暫停一千秒,什麼事都不作,一千秒以後把命令窗口恢復回來
[root@localhost ~]# sleep 1000
^Z
[1]+  已中止               sleep 1000
[root@localhost ~]# jobs
[1]+  已中止               sleep 1000
[root@localhost ~]# sleep 200
^Z
[2]+  已中止               sleep 200
[root@localhost ~]# jobs
[1]-  已中止               sleep 1000
[2]+  已中止               sleep 200
[root@localhost ~]# fg
sleep 200
^Z
[2]+  已中止               sleep 200
在調到先後臺運行的時候,不指定id號,就是默認最後一條執行命令
  • & 符號,把任務丟到後臺去執行
[root@localhost ~]# sleep 100 &
[3] 2239
[root@localhost ~]# jobs
[1]   運行中               sleep 100 &
[root@localhost ~]#

在打開另外一終端,jobs命令,是查看不到執行當前終端的任務bash

可是在另外一個終端,能夠查看到進程ps aux |grep sleep命令行

```
[root@localhost ~]# ps aux |grep sleep
root      2235  0.0  0.0 107892   624 pts/0    T    23:20   0:00 sleep 1000
root      2236  0.0  0.0 107892   620 pts/0    T    23:20   0:00 sleep 200
root      2264  0.0  0.0 112656   984 pts/1    R+   23:31   0:00 grep --color=auto slee
[root@localhost ~]# 
```
相關文章
相關標籤/搜索