标签:linux; jobs; fg; bg; ctrl+c; ctrl+z;

描述:

进程前后台操作用到以下命令或按键:

Command Description
Ctrl+C 终止并退出前台命令的执行,回到SHELL
Ctrl+Z 暂停前台命令的执行,将该进程放入后台,回到SHELL
jobs 查看当前在后台执行的命令,可查看命令进程号码
& 运行命令时,在命令末尾加上&可让命令在后台执行
fg N 将命令进程号码为N的命令进程放到前台执行,同%N
bg N 将命令进程号码为N的命令进程放到后台执行

附录

Ex1:Ctrl C

ping 后直接退出,无进程

root@crackcreed:~# ping google.com
PING google.com (172.217.24.142) 56(84) bytes of data.
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=1 ttl=58 time=0.651 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=2 ttl=58 time=0.750 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=3 ttl=58 time=0.713 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 0.651/0.704/0.750/0.051 ms

root@crackcreed:~# ps -auxww |grep google
root     23946  0.0  0.2  12728  2104 pts/0    S+   01:41   0:00 grep google

Ex2:Ctrl Z

ping 后切到shell,后台进程显示stopped

root@crackcreed:~# ping google.com
PING google.com (172.217.24.142) 56(84) bytes of data.
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=1 ttl=58 time=0.644 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=2 ttl=58 time=0.741 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=3 ttl=58 time=0.728 ms
^Z
[1]+  Stopped                 ping google.com
root@crackcreed:~# ps -auxww |grep google
root     23952  0.0  0.1  14772  1900 pts/0    T    01:41   0:00 ping google.com
root     23964  0.0  0.2  12728  2196 pts/0    S+   01:41   0:00 grep google
root@crackcreed:~# jobs
[1]+  Stopped                 ping google.com

Ex3: fg

fg 1,将Ex2中job[1]放到前台执行

root@crackcreed:~# fg 1
ping google.com
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=4 ttl=58 time=0.678 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=5 ttl=58 time=0.730 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=6 ttl=58 time=0.763 ms

^Z
[1]+  Stopped                 ping google.com
root@crackcreed:~# jobs
[1]+  Stopped                 ping google.com

Ex4: bg

bg 1,将job[1]放到后台执行,相当于 &

root@crackcreed:~# bg 1
[1]+ ping google.com &
root@crackcreed:~# 64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=21 ttl=58 time=0.677 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=22 ttl=58 time=0.701 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=23 ttl=58 time=0.717 ms
64 bytes from nrt20s01-in-f142.1e100.net (172.217.24.142): icmp_seq=24 ttl=58 time=0.716 ms
...
root@crackcreed:~#jobs
[1]+  Running                 ping google.com &