管道符、重定向

管道

管道实际上就是进程之间的一个通信工具,那么用在Linux命令中主要是方便两条命令互相之间可以相互通信。

管道符

管道符(pipe)用“|”这个界定符号表示,如果需要对linux命令的输出结果进行再次处理,就可以使用管道符+管道命令解决。作用主要是把两个应用程序连接在一起,然后把第一个应用程序的输出,作为第二个应用程序的输入。
1、管道命令只处理前一个命令正确输出,不处理错误输出。
2、管道命令右边命令,必须能够接收标准输入流命令才行

ex

[root@node1 ~]# ls
anaconda-ks.cfg              testScp.txt
apache-tomcat-8.5.55         test.zip
apache-tomcat-8.5.55.tar.gz  大数据入门指南_v1.0_.pdf
Deep_Learning_and_Spark.pdf
[root@node1 ~]# cat testScp.txt  | grep a
阿eqeqwdjaoJ
[root@node1 ~]# vi testScp.txt 
[root@node1 ~]# 

grep

grep是一个最初用于Unix操作系统的命令行工具。在给出文件列表或标准输入后,grep会对匹配一个或多个正则表达式的文本进行搜索,并只输出匹配(或者不匹配)的行或文本

ex:

[root@node1 ~]# grep Apple fruitList.txt 
Apple [ˈæpl] 苹果I
[root@node1 ~]# grep apple fruitList.txt 
[root@node1 ~]# 

在这个例子里,grep会返回“fruitlist.txt”中所有包含“Apple”的文本行。要注意的是,grep不会返回匹配“apple”(A字母大写)的文本行,因为grep默认情况下是大小写敏感的。但grep接受参数来改变或增加一些特别的功能。

grep -i apple fruitlist.txt
这个命令会返回所有匹配“apple”、“Apple”、“apPLE”或其它混合大小写的拼写。

重定向

输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中.

标准输出重定向command >file以覆盖的方式,把 command 的正确输出结果输出到 file 文件中。
command >>file以追加的方式,把 command 的正确输出结果输出到 file 文件中。
标准错误输出重定向command 2>file以覆盖的方式,把 command 的错误信息输出到 file 文件中。
command 2>>file以追加的方式,把 command 的错误信息输出到 file 文件中。
正确输出和错误信息同时保存command >file 2>&1以覆盖的方式,把正确输出和错误信息同时保存到同一个文件(file)中。
command >>file 2>&1以追加的方式,把正确输出和错误信息同时保存到同一个文件(file)中。
command >file1 2>file2以覆盖的方式,把正确的输出结果输出到 file1 文件中,把错误信息输出到 file2 文件中。
command >>file1 2>>file2以追加的方式,把正确的输出结果输出到 file1 文件中,把错误信息输出到 file2 文件中。
command >file 2>file【不推荐】这两种写法会导致 file 被打开两次,引起资源竞争,所以 stdout 和 stderr 会互相覆盖。

标准输出重定向

[root@node1 ~]# ping www.baidu.com > f1.txt
^Z
[2]+  Stopped                 ping www.baidu.com > f1.txt
[root@node1 ~]# ls
anaconda-ks.cfg              fruitList.txt
apache-tomcat-8.5.55         testScp.txt
apache-tomcat-8.5.55.tar.gz  test.zip
Deep_Learning_and_Spark.pdf  大数据入门指南_v1.0_.pdf
f1.txt
[root@node1 ~]# cat f
f1.txt         fruitList.txt  
[root@node1 ~]# cat f1.txt 
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=33.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=32.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=33.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=128 time=32.7 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=128 time=32.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=6 ttl=128 time=33.5 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=7 ttl=128 time=32.7 ms
[root@node1 ~]# ping www.baidu.com >> f1.txt
^Z
[3]+  Stopped                 ping www.baidu.com >> f1.txt
[root@node1 ~]# cat f1.txt 
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=33.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=32.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=128 time=33.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=128 time=32.7 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=5 ttl=128 time=32.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=6 ttl=128 time=33.5 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=7 ttl=128 time=32.7 ms
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=128 time=30.4 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=128 time=29.0 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=3 ttl=128 time=28.6 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=4 ttl=128 time=52.2 ms
[root@node1 ~]# 

标准错误输出重定向

[root@node1 ~]# ping www.dqeerwqd.com 2>f2.txt #覆盖
[root@node1 ~]# cat f2.txt 
ping: www.dqeerwqd.com: Name or service not known
[root@node1 ~]# ping www.dqeerwqd.com 2>>f2.txt #追加
[root@node1 ~]# cat f2.txt 
ping: www.dqeerwqd.com: Name or service not known
ping: www.dqeerwqd.com: Name or service not known
[root@node1 ~]# 

PS命令

ps命令是Process Status的缩写,用来列出系统中当前运行的进程。使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵死、哪些进程占用了过多的资源等等。ps命令所列出的进行是当前进程的快照,也就是并不是动态的,而是执行该命令时那一时刻进行的状态。而经常和ps一起结合使用的杀死进程的是kill命令
链接:https://www.jianshu.com/p/943b90150c10

ps -A  显示所有进程
ps -e  和-A功能一样
ps -H  显示树状结构,表示程序间的相互关系
ps -f  全格式显示进程

ps a   显示当前终端下执行的进程
ps c   显示进程的真实名称
ps e   列出程序所使用的环境变量
ps f   用ASCII字符显示树状结构,表达程序间的相互关系
ps x   显示所有进程,无论是否运行在终端上
ps u   显示用户相关的进程或者与用户相关的属性
ps r   只显示正在运行的进程
[root@node1 ~]# ps -ef |grep ping
root       1876   1496  0 09:18 pts/0    00:00:00 ping www.qq.com
root       1899   1496  0 09:48 pts/0    00:00:00 ping www.baidu.com
root       1902   1496  0 09:52 pts/0    00:00:00 ping www.baidu.com
root       1927   1496  0 10:17 pts/0    00:00:00 grep --color=auto ping
[root@node1 ~]# ps -u kkk#查看指定用户下的进程
   PID TTY          TIME CMD
[root@node1 ~]# ps -L 1876
   PID    LWP TTY      STAT   TIME COMMAND
  1876   1876 pts/0    T      0:00 ping www.qq.com
[root@node1 ~]# ps -axf#树形显示
#PID表示进程端口号,PPID表示当前进程的父进程的端口号
   PID TTY      STAT   TIME COMMAND
     2 ?        S      0:00 [kthreadd]
     4 ?        S<     0:00  \_ [kworker/0:0H]
     6 ?        S      0:00  \_ [ksoftirqd/0]
     7 ?        S      0:01  \_ [migration/0]
     8 ?        S      0:00  \_ [rcu_bh]
     9 ?        S      0:01  \_ [rcu_sched]
    10 ?        S<     0:00  \_ [lru-add-drain]
    11 ?        S      0:00  \_ [watchdog/0]
    12 ?        S      0:00  \_ [watchdog/1]
    13 ?        S      0:01  \_ [migration/1]
    14 ?        S      0:00  \_ [ksoftirqd/1]
[root@node1 ~]#ps -aux --sort -pcpu:按照cpu进行排序显示
[root@node1 ~]#ps -aux --sort -pmem:按照mem进行排序显示

top命令

top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器

top命令输出结果中,统计信息区前五行是系统整体的统计信息。
其余参数查阅:https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html

[root@node1 ~]# top
top - 10:48:12 up 1 day,  1:09,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 207 total,   1 running, 202 sleeping,   4 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1067384 total,   766108 free,   186396 used,   114880 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   747720 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
  1935 root      20   0  162100   2348   1572 R   0.3  0.2   0:00.05 top         
     1 root      20   0  193568   6632   4160 S   0.0  0.6   0:01.63 systemd     
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 kthreadd    
     4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H
     6 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/0 
     7 root      rt   0       0      0      0 S   0.0  0.0   0:01.78 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:01.31 rcu_sched   
    10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-dra+
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.47 watchdog/0  
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.42 watchdog/1  
    13 root      rt   0       0      0      0 S   0.0  0.0   0:01.78 migration/1 
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.01 ksoftirqd/1 
    15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0 
    16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H
    17 root      rt   0       0      0      0 S   0.0  0.0   0:00.40 watchdog/2  
    18 root      rt   0       0      0      0 S   0.0  0.0   0:01.77 migration/2 
    19 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/2 
    20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0 
    21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/2:0H
    22 root      rt   0       0      0      0 S   0.0  0.0   0:00.40 watchdog/3  
    23 root      rt   0       0      0      0 S   0.0  0.0   0:01.77 migration/3 
    24 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/3 
    25 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0 
    26 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/3:0H
    27 root      rt   0       0      0      0 S   0.0  0.0   0:00.42 watchdog/4  
    28 root      rt   0       0      0      0 S   0.0  0.0   0:01.77 migration/4 
    29 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/4 
    30 root      20   0       0      0      0 S   0.0  0.0   0:03.43 kworker/4:0 

后台进程

1.在命令后面添加一个&符号,可以让命令在后台运行,同时给出一个端口号.

[root@node1 ~]# ping google.com >f1.txt&
[6] 1938

2.jobs -l - 可以查看当前的后台进程但是只有当前用户界面可以获取到

[root@node1 ~]# jobs -l
[1]   1876 Stopped                 ping www.qq.com
      1877                       | grep --color=auto /ggg
[2]   1899 Stopped                 ping www.baidu.com > f1.txt
[3]   1902 Stopped                 ping www.baidu.com >> f1.txt
[4]-  1935 Stopped (signal)        top
[5]+  1937 Stopped                 ping google.com > f1.txt
[6]   1938 Running                 ping google.com > f1.txt &
[root@node1 ~]# 

3.nohup,不挂断地运行命令,必须和&一块使用

[root@node1 ~]# nohup ping google.com >f1.txt &
[11] 1945
[root@node1 ~]# nohup: ignoring input and redirecting stderr to stdout
nohup ping googl
[root@node1 ~]# jobs -l
[1]   1876 Stopped                 ping www.qq.com
      1877                       | grep --color=auto /ggg
[2]   1899 Stopped                 ping www.baidu.com > f1.txt
[3]   1902 Stopped                 ping www.baidu.com >> f1.txt
[4]   1935 Stopped (signal)        top
[5]-  1937 Stopped                 ping google.com > f1.txt
[6]   1938 Running                 ping google.com > f1.txt &
[8]   1942 Running                 nohup ping google.com > f1.txt &
[9]+  1943 Stopped                 vi f1.txt
[10]   1944 Running                 nohup ping google.com > f1.txt &
[11]   1945 Running                 nohup ping google.com > f1.txt &

4.kill命令,Linux中的kill命令用来终止指定的进程(terminate a process)的运行

  • kill 12345 杀死进程
  • kill -KILL 123456 强制杀死进程
  • kill -9 123456 彻底杀死进程
  • kill -l显示信号
  • 杀死指定用户所有进程;
    kill -9 $(ps -ef | grep hnlinux) //方法一 过滤出hnlinux用户进程
    kill -u hnlinux //方法二
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值