& fg jobs bg
&
执行程序的后面加&
可以将程序转到后台(这个后台是当前会话的后台,并不是守护进程)执行,即$./a.out &
,这样我们在打开诸如$gedit test.txt
的时候可以写成$gedit test.txt &
以防止它占用终端。后台进程也叫后台作业,执行后台进程的时候会有一个后台作业的编号:
$./a.out &
[1] 3054 #后台作业1的编号是3054
jobs
$jobs
用来查看当前会话的后台作业(后台进程组):
$./a.out &
[1] 3102
$./a.out &
[2] 3103
$./a.out &
[3] 3104
$./a.out &
[4] 3105
$./a.out &
[5] 3106
161029 $jobs
[1] Running ./a.out &
[2] Running ./a.out &
[3] Running ./a.out &
[4]- Running ./a.out &
[5]+ Running ./a.out &
fg
$fg [作业号]
可以将后台进程变成前台进程
$fg 1
./a.out
^C
$jobs
[2] Running ./a.out &
[3] Running ./a.out &
[4]- Running ./a.out &
[5]+ Running ./a.out &
$fg 2
./a.out
^C
$jobs
[3] Running ./a.out &
[4]- Running ./a.out &
[5]+ Running ./a.out &
Ctrl+z bg
Ctrl+z
将当前前台的进程在后台挂起,$bg [任务号]
可以将在后台挂起的进程运行起来
$jobs
[3] Running ./a.out &
[4]- Running ./a.out &
[5]+ Running ./a.out &
$fg 3
./a.out
^Z
[3]+ Stopped ./a.out
161029 $bg 3
[3]+ ./a.out &