发布于 5年前

Linux任务的前后台管理

我们知道,Linux 是一个多任务的操作系统,也就是说,在同一时间,系统可以运行多个任务。在带界面的 Linux 发行版下,我们可以很轻松通过鼠标来进行多任务的切换。但是,在终端下,操作几乎是脱离鼠标的,如何进行任务的前后台管理呢?

对于任务的管理,我们一般有如下几个需求:

  • 将进程切换到后台
  • 将进程切换到前台
  • 查看后台任务
  • 终止后台任务

为了演示这几个需求,我们搬出伟大的 hello world 程序:

#include <stdio.h>

int main()
{
    while (1) {
        printf("hello world!\n");
        sleep(1);
    }
}

这个程序每隔 1s 就会输出一句 hello world 。为了后面的演示,将它编译并复制多份样本:hello1, hello2, hello3。

让程序在后台运行,我们第一时间想到的就是 & 命令。我们可以先在后台运行这三个程序。

[alvin@VM_0_16_centos test]$ ./hello1 > test1.txt &
[1] 1788
[alvin@VM_0_16_centos test]$ ./hello2 > test2.txt &
[2] 1801
[alvin@VM_0_16_centos test]$ ./hello3 > test3.txt &
[3] 1844

现在有三个样本在后台跑了。我们如何查看这几个后台任务呢?可以使用 jobs 命令。

[alvin@VM_0_16_centos test]$ jobs -l
[1]   1788 Running                 ./hello1 > test1.txt &
[2]-  1801 Running                 ./hello2 > test2.txt &
[3]+  1844 Running                 ./hello3 > test3.txt &

如果我们想把 hello2 调至前台运行,我们可以这样操作:

[alvin@VM_0_16_centos test]$ fg %2
./hello2 > test2.txt

其中,%后面跟的是后台任务的序列,而不是进程ID。如果只有 fg 命令( bg 命令也一样),而不跟参数,那么将操作的是后台任务列表里的第一个任务,专业名词叫当前任务 。

我们会发现,这时程序会一直卡在终端。这时,我们可以使用 ctrl+z 将它再次切到后台运行。

^Z
[2]+  Stopped                 ./hello2 > test2.txt
[alvin@VM_0_16_centos test]$ jobs -l
[1]   1788 Running                 ./hello1 > test1.txt &
[2]+  1801 Stopped                 ./hello2 > test2.txt
[3]-  1844 Running                 ./hello3 > test3.txt &

但是,我们会发现,test2 进程变成了 stopped 的状态,我们也可以在后台进程列表里看到它的状态。这也是 ctrl+z 命令的特点:将进程切换到后台,并停止运行。

如果我们想让它恢复运行,我们就可以使用 bg 命令了。

[alvin@VM_0_16_centos test]$ bg %2
[2]+ ./hello2 > test2.txt &
[alvin@VM_0_16_centos test]$ jobs -l
[1]   1788 Running                 ./hello1 > test1.txt &
[2]-  1801 Running                 ./hello2 > test2.txt &
[3]+  1844 Running                 ./hello3 > test3.txt &

如果我们想杀死某个后台进程,我们可以使用 kill 命令。kill 命令的用法有两种:

  1. kill pid
  2. kill %N

例如,我们要杀死 hello2 进程的话,可以这样操作:

1. kill 1801
2. kill %2

执行完毕之后,它的状态将变成 terminated 状态:

[alvin@VM_0_16_centos test]$ kill 1801
[alvin@VM_0_16_centos test]$ jobs -l
[1]   1788 Running                 ./hello1 > test1.txt &
[2]-  1801 Terminated              ./hello2 > test2.txt
[3]+  1844 Running                 ./hello3 > test3.txt &

前台、后台任务确实可以给日常操作带来方便。因为,我们在日常操作中肯定会遇到同一时间要进行多个操作。这个时候如果不使用前台任务和后台任务,那么将要花费很多时间。熟练运用前台和后台任务能达到事半功倍。

原文地址:https://mp.weixin.qq.com/s/y8D-ZP10d5jodB8q8iAWIg

©2020 edoou.com   京ICP备16001874号-3