Linux任务的前后台管理

bb

我们知道,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 &
[11788
[alvin@VM_0_16_centos test]$ ./hello2 > test2.txt &
[21801
[alvin@VM_0_16_centos test]$ ./hello3 > test3.txt &
[31844

现在有三个样本在后台跑了。我们如何查看这几个后台任务呢?可以使用 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 进程的话,可以这样操作:

1kill 1801
2kill %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 &

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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69908604/viewspace-2636301/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/69908604/viewspace-2636301/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值