& 表示任务在后台执行,如要在后台运行ping www.baidu.com,则有 ping www.baidu.com &,将这个任务放到后台去执行,关闭终端也不影响这个任务的正常执行。但是要求这个任务输出内容到标准输出中(例如ping、echo或ls),即使使用了&,也会等待这些输出任务在前台运行完毕
在前台运行:
ctrl+z组合键可以让前台运行的任务暂停,仅仅是暂停,而不是将任务终止
ctrc+c组合键是终止任务
[root@y_zilong ~]# ping www.baidu.com
PING www.wshifen.com (104.193.88.77) 56(84) bytes of data.
64 bytes from 104.193.88.77 (104.193.88.77): icmp_seq=1 ttl=128 time=184 ms
64 bytes from 104.193.88.77 (104.193.88.77): icmp_seq=2 ttl=128 time=562 ms
^Z
[1]+ Stopped ping www.baidu.com
[root@y_zilong ~]# ps aux|grep ping
gdm 6133 0.0 0.2 283396 4876 tty1 Sl+ 04:54 0:00 /usr/libexec/gsd-housekeeping
root 7859 0.0 0.3 366164 7324 tty2 Sl+ 04:55 0:00 /usr/libexec/gsd-housekeeping
root 11025 0.0 0.2 54588 4840 pts/1 T 07:46 0:00 ping www.baidu.com
root 11030 0.0 0.0 12112 1112 pts/1 S+ 07:46 0:00 grep --color=auto ping
[root@y_zilong ~]#
当使用&将一个进程放置到后台运行的时候,可以使用进程ID来暂停、恢复或者终止对应的进程
1、jobs #查看当前有多少在后台运行的命令
2、fg #将后台中的命令调至前台继续运行
3、bg #将一个在后台暂停的命令,变成继续执行
4、ps -aux #显示进程所有者的信息
4、kill #发送信号给一个或多个进程(经常用来杀死一个进程)
[root@y_zilong ~]# ps aux |grep ping #查看进程所有者的信息
gdm 6133 0.0 0.2 283396 4876 tty1 Sl+ 04:54 0:00 /usr/libexec/gsd-housekeeping
root 7859 0.0 0.3 366164 7324 tty2 Sl+ 04:55 0:00 /usr/libexec/gsd-housekeeping
root 11025 0.0 0.2 54588 4724 pts/1 T 07:46 0:00 ping www.baidu.com
root 11149 0.0 0.0 12112 1100 pts/1 S+ 07:50 0:00 grep --color=auto ping
[root@y_zilong ~]# jobs #查看当前有多少在后台运行的命令
[1]+ Stopped ping www.baidu.com
[root@y_zilong ~]# bg #将一个后台暂停的命令,变成继续执行
[1]+ ping www.baidu.com &
[root@y_zilong ~]# 64 bytes from 104.193.88.77 (104.193.88.77): icmp_seq=3 ttl=128 time=209 ms
64 bytes from 104.193.88.77 (104.193.88.77): icmp_seq=4 ttl=128 time=158 ms
64 bytes from 104.193.88.77 (104.193.88.77): icmp_seq=5 ttl=128 time=158 ms
打开另一窗口
[root@y_zilong ~]# ps aux|grep ping
gdm 6133 0.0 0.2 283396 4876 tty1 Sl+ 04:54 0:00 /usr/libexec/gsd-housekeeping
root 7859 0.0 0.3 366164 7316 tty2 Sl+ 04:55 0:00 /usr/libexec/gsd-housekeeping
root 11025 0.0 0.2 54588 4724 pts/1 S 07:46 0:00 ping www.baidu.com
root 11193 0.0 0.0 12112 1088 pts/2 S+ 07:54 0:00 grep --color=auto ping
[root@y_zilong ~]# kill 11025 #杀死正在运行的进程
[root@y_zilong ~]#
&& 表示前一条命令执行成功时,才执行后一条命令 ,如 echo '1‘ && echo '2'
[root@y_zilong ~]# echo '1' && echo '2'
1
2
[root@y_zilong ~]#
| 表示管道,上一条命令的输出,作为下一条命令参数,如 echo "yes\nno" | wc -l
[root@y_zilong ~]# echo -e "yes\nno"
yes
no
[root@y_zilong ~]# echo -e "yes\nno" | wc -l
2
[root@y_zilong ~]#
|| 表示上一条命令执行失败后,才执行下一条命令,如 cat test1.txt || echo "aa”
[root@y_zilong ~]# cat test.txt
test
[root@y_zilong ~]# cat test.txt || echo "aa"
test
[root@y_zilong ~]# cat test1.txt || echo "aa"
cat: test1.txt: No such file or directory
aa
[root@y_zilong ~]#