0036-【Linux系统】-如何命令后台执行

当命令需要长时间运行,或者网络不稳定,担心当前窗口会断线的情况下,往往需要后台执行,以免窗口不小心关闭,导致命令中断。

知识点
  • & 指定命令后台运行
  • fg 将后台命令放到前台
  • Ctrl Z 将命令暂停
  • bg 将暂停后的命令放到后台,并重新激活
  • % 将最经一次的后台命令,放到前台,与fg功效一样
  • jobs 查看当前窗口下有哪些运行的后台命令

注意:这些命令,仅限在当前的窗口有效,在窗口关闭,或其他窗口关闭无效。

1. 测试例子,使用ping的方法,查看网络是否连接正常

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:30  forstart
$ping www.baidu.com
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
64 bytes from 163.177.151.110 (163.177.151.110): icmp_seq=1 ttl=128 time=6.62 ms
64 bytes from 163.177.151.110 (163.177.151.110): icmp_seq=2 ttl=128 time=7.16 ms

2. 命令尾部加&,可以让命令在后台执行

有个弊端是,即使命令在后台运行,但标准输出还是在前台。

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:31  forstart
$PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=1 ttl=128 time=7.35 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=2 ttl=128 time=7.42 ms

3. 将标准输出放到文件中,的方式执行

>为将标准输入重定向到文件中,2>&1将标准错误也合并到标准输出中
将命令放后台后,出现任务好 3145

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:36  forstart
$ping www.baidu.com > baidu.txt 2>&1 &
[1] 3145

4. jobs查看后台运行中的命令

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:40  forstart
$jobs
[1]+  Running                 ping www.baidu.com > baidu.txt 2>&1 &

5. fg将命令调至前台

窗口最后一行为空,表示一致在执行中

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:42  forstart
$fg
ping www.baidu.com > baidu.txt 2>&1

6. 将Ctrl Z命令暂停

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:42  forstart
$fg
ping www.baidu.com > baidu.txt 2>&1
^Z
[1]+  Stopped                 ping www.baidu.com > baidu.txt 2>&1

7. bg将命令放置后台

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:44  forstart
$bg
[1]+ ping www.baidu.com > baidu.txt 2>&1 &

8. 再次jobs查看后台执行中的任务

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:44  forstart
$jobs
[1]+  Running                 ping www.baidu.com > baidu.txt 2>&1 &

9.使用%号,将后台命令调至前台

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:44  forstart
$%
ping www.baidu.com > baidu.txt 2>&1

10.再次将命令暂停放后台

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:44  forstart
$%
ping www.baidu.com > baidu.txt 2>&1
^Z
[1]+  Stopped                 ping www.baidu.com > baidu.txt 2>&1
toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:46  forstart
$bg
[1]+ ping www.baidu.com > baidu.txt 2>&1 &
toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:46  forstart
$jobs
[1]+  Running                 ping www.baidu.com > baidu.txt 2>&1 &

11. 在多个后台任务时,根据任务号,进行将后台命令调至前台。任务号为最左边的1,2,3

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:46  forstart
$%1
ping www.baidu.com > baidu.txt 2>&1

12. 使用Ctrl C 提前结束命令,查看输出内容

toucan@tssys /home/toucan/Downloads  Sat Jun 09 11:46  forstart
$%1
ping www.baidu.com > baidu.txt 2>&1
^Ctoucan@tssys /home/toucan/Downloads  Sat Jun 09 11:49  forstart
$head baidu.txt
PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.
64 bytes from 163.177.151.110 (163.177.151.110): icmp_seq=1 ttl=128 time=6.62 ms
64 bytes from 163.177.151.110 (163.177.151.110): icmp_seq=2 ttl=128 time=7.10 ms

13. %的具体使用方法

这里写图片描述

猜你喜欢

转载自blog.csdn.net/leadingsci/article/details/80631642