linux前后台任务的切换以及执行暂停

时间:2021-08-14 17:21:55

command & 把command命令放到后台执行
ctrl+z 暂停该任务,并且放到后台
jobs 查看任务
bg n 把jobs号码为n的任务放到后台执行
fg n 把jobs号码为n的任务放到前台执行
kill n 把jobs号码为n的任务杀死,注意n是对应的jobs号,不是pid号码

[00:17:39] gcc whiletest.c //打印9到1
[00:17:43] ./a.out  //直接运行
this is 9
this is 8
this is 7
^Z      //ctrl+z 暂停
[1]+  Stopped                 ./a.out
[00:17:48] jobs //查看任务
[1]+  Stopped                 ./a.out
[00:17:53] fg 1 //把任务1放到前台执行
./a.out
this is 6
this is 5
this is 4
^Z
[1]+  Stopped                 ./a.out
[00:18:31] bg 1     //把任务放到后台,因为有打印语句,所以影响到终端了
[1]+ ./a.out &
[00:19:08] this is 3
this is 2
this is 1
this is 0

[1]+  Done                    ./a.out
[00:19:13] jobs
[00:20:49] ./a.out 
this is 9
this is 8
this is 7
^Z
[1]+  Stopped                 ./a.out
[00:21:05] bg 1
[1]+ ./a.out &
[00:21:18] this is 6
this is 5
this is 4
this is 3
this is 2
this is 1
this is 0

[00:21:25] jobs
[1]+  Done                    ./a.out
[00:21:32] jobs
[00:21:37] ./a.out
this is 9
this is 8
^Z
[1]+  Stopped                 ./a.out
[00:21:44] kill %1  //注意需要%号

[1]+  Stopped                 ./a.out
[00:21:48] jobs
[1]+  Terminated              ./a.out
[00:21:50] jobs
[00:21:53]