Linxu中命令的执行(前台、后台)

发表于:2018-6-27 09:56

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:NealCaffrey_    来源:51testing软件测试网采编

  今天我要分享的知识是如何建立进程。在Linux中我们应该如何进程呢,让我们好好来探究一下吧。
  示例一:
  这个例子就是建立了一个后台进程
  [root@instructor Desktop]# ping 127.0.0.1&
  [1] 16885
  [root@instructor Desktop]# PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
  64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.353 ms
  64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=1.05 ms
  64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.065 ms
  64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.069 ms
  64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.066 ms
  64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.146 ms
  fg
  ping 127.0.0.1
  64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.067 ms
  64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.070 ms
  64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.069 ms
  64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.072 ms
  ^C
  --- 127.0.0.1 ping statistics ---
  10 packets transmitted, 10 received, 0% packet loss, time 9471ms
  rtt min/avg/max/mdev = 0.065/0.202/1.050/0.295 ms
  [root@instructor Desktop]# 
  其实在Linux中有两种建立进程的方式,一个是在前台运行程序,另一个是在后台运行程序。
  一、在前台运行程序,就是命令的正常执行,不需要添加任何符号或命名。
  示例二:
  [root@instructor Desktop]# ping 127.0.0.1
  PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
  64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.102 ms
  64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.070 ms
  64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.071 ms
  64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.082 ms
  ^C
  --- 127.0.0.1 ping statistics ---
  4 packets transmitted, 4 received, 0% packet loss, time 3401ms
  rtt min/avg/max/mdev = 0.070/0.081/0.102/0.014 ms
  [root@instructor Desktop]# 
  二、后台运行进程
  在文章的开头我们举了第一个例子,我们得知通过在运行的指令末尾添加”&“符号可以让命令在后台运行
  示例三:
  [root@instructor Desktop]# ping 192.168.0.254&
  [1] 17077
  [root@instructor Desktop]# PING 192.168.0.254 (192.168.0.254) 56(84) bytes of data.
  64 bytes from 192.168.0.254: icmp_seq=1 ttl=64 time=0.205 ms
  64 bytes from 192.168.0.254: icmp_seq=2 ttl=64 time=0.112 ms
  64 bytes from 192.168.0.254: icmp_seq=3 ttl=64 time=0.100 ms
  64 bytes from 192.168.0.254: icmp_seq=4 ttl=64 time=0.065 ms
  ^C
  [root@instructor Desktop]# 64 bytes from 192.168.0.254: icmp_seq=5 ttl=64 time=0.107 ms
  64 bytes from 192.168.0.254: icmp_seq=6 ttl=64 time=0.131 ms
  64 bytes from 192.168.0.254: icmp_seq=7 ttl=64 time=0.110 ms
  64 bytes from 192.168.0.254: icmp_seq=8 ttl=64 time=0.980 ms
  64 bytes from 192.168.0.254: icmp_seq=9 ttl=64 time=0.111 ms
  fg
  ping 192.168.0.254
  64 bytes from 192.168.0.254: icmp_seq=10 ttl=64 time=0.100 ms
  64 bytes from 192.168.0.254: icmp_seq=11 ttl=64 time=0.110 ms
  64 bytes from 192.168.0.254: icmp_seq=12 ttl=64 time=0.070 ms
  ^C
  --- 192.168.0.254 ping statistics ---
  12 packets transmitted, 12 received, 0% packet loss, time 11461ms
  rtt min/avg/max/mdev = 0.065/0.183/0.980/0.242 ms
  [root@instructor Desktop]#
 
  三、关于后台进程控制的命令和快捷键
  1、 Ctrl-Z
    Ctrl-Z可以将一个正在前台执行的命令放到后台,并且暂停
  2、bg
  bg将一个在后台暂停的命令,变成继续执行
  如果后台中有多个命令,可以用bg
   %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)
  3、fg
  如果想把它调回到前台运行,可以用
  4、jobs
  查看当前有多少在后台运行的命令
  示例四:
  假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctrl-Z ,挂起这个程序,然后可以看到系统提示(方括号中的是作业号):
  [1]+ Stopped /root/bin/rsync.sh
  然后我们可以把程序调度到后台执行:(bg 后面的数字为作业号)
  #bg 1
  [1]+ /root/bin/rsync.sh &
  用 jobs 命令查看正在运行的任务:
  #jobs
  [1]+ Running /root/bin/rsync.sh &
  如果想把它调回到前台运行,可以用
  #fg 1
  /root/bin/rsync.sh
  这样,你在控制台上就只能等待这个任务完成了。
  fg、bg、jobs、&、ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的。



上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号