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 ~]# 
```
相关文章
相关标签/搜索