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

本文发布时间: 2019-Mar-22
距离上一篇文章已经有半个月的时间了,感觉这段时间对不起自己呀。因为现在我开始学习Java了,以后有时间和大家交流一个学习经验。今天我要分享的知识是如何建立进程。在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 ms64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=1.05 ms64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.065 ms64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.069 ms64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.066 ms64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.146 msfgping 127.0.0.164 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.067 ms64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.070 ms64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.069 ms64 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 9471msrtt min/avg/max/mdev = 0.065/0.202/1.050/0.295 ms[root@instructor Desktop]#其实在Linux中有两种建立进程的方式,一个是在前台运行程序,另一个是在后台运行程序。一、在前台运行程序,就是命令的正常执行,不需要添加任何符号或命名。示例二:[root@instructor Desktop]# ping 127.0.0.1PING 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 ms64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.070 ms64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.071 ms64 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 3401msrtt 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 ms64 bytes from 192.168.0.254: icmp_seq=2 ttl=64 time=0.112 ms64 bytes from 192.168.0.254: icmp_seq=3 ttl=64 time=0.100 ms64 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 ms64 bytes from 192.168.0.254: icmp_seq=6 ttl=64 time=0.131 ms64 bytes from 192.168.0.254: icmp_seq=7 ttl=64 time=0.110 ms64 bytes from 192.168.0.254: icmp_seq=8 ttl=64 time=0.980 ms64 bytes from 192.168.0.254: icmp_seq=9 ttl=64 time=0.111 msfgping 192.168.0.25464 bytes from 192.168.0.254: icmp_seq=10 ttl=64 time=0.100 ms64 bytes from 192.168.0.254: icmp_seq=11 ttl=64 time=0.110 ms64 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 11461msrtt min/avg/max/mdev = 0.065/0.183/0.980/0.242 ms[root@instructor Desktop]#三、关于后台进程控制的命令和快捷键1、 Ctrl-ZCtrl-Z可以将一个正在前台执行的命令放到后台,并且暂停2、bgbg将一个在后台暂停的命令,变成继续执行如果后台中有多个命令,可以用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都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表