1、把进程放入后台
》tar -zcf etc.tar.gz /etc &
#该方法把tar 进程放入后台时,tar进程并未停止,还在后台运行
[root@localhost ~]# tar -zcf testfile /etc &
[1] 3066
[root@localhost ~]# tar: 从成员名中删除开头的“/”
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# tar -zcf testfile /etc &tar: 从硬连接目标中删除开头的“/”
[1]+ Done tar -zcf testfile /etc
》按下Ctrl+z 的方式将某个进程放入后台,比如把top进程放入后台
[root@localhost ~]# top
#在top 命令执行的过程中,按下Ctrl+z快捷键,但该方法放入后台的进程会终止运行
注意:
Windows中,把窗口缩小,就是把进程放入后台;
Linux图形界面中,和Windows一样,但是命令行界面,通过
在某个命令后加& 符合,即可把该进程放入后台,或者通过执行top命令时,按下Ctrl+z 实现
2、查看后台的工作
[root@localhost ~]# jobs [-l ]
选项:
-l :显示工作的PID
注: + 号代表最近一个放入后台的工作,也是工作恢复时,默认恢复的工作
- 号代表倒数第二个放入后台的工作
[root@localhost ~]# tar -zcf testfile /etc &
[1] 3114
[root@localhost ~]# tar: 从成员名中删除开头的“/”
[root@localhost ~]#
[root@localhost ~]# tar -zcf testfile /etc &tar: 从硬连接目标中删除开头的“/”
jobs -l
[1]+ 3114 Done tar -zcf testfile /etc
[root@localhost ~]# top
top - 01:10:31 up 57 min, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 171 total, 1 running, 170 sleeping, 0 stopped, 0 zombie
Cpu(s): 1.5%us, 1.8%sy, 0.0%ni, 95.4%id, 0.8%wa, 0.1%hi, 0.4%si, 0.0%st
Mem: 1906188k total, 912216k used, 993972k free, 30624k buffers
Swap: 2097148k total, 0k used, 2097148k free, 521028k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3129 root 20 0 15024 1164 832 R 2.9 0.1 0:00.21 top
1 root 20 0 19364 1568 1240 S 0.0 0.1 0:03.52 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.14 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
7 root 20 0 0 0 0 S 0.0 0.0 0:07.77 events/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events/0
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_long/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_power_ef
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
16 root 20 0 0 0 0 S 0.0 0.0 0:00.18 sync_supers
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
19 root 20 0 0 0 0 S 0.0 0.0 0:00.89 kblockd/0
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
24 root 20 0 0 0 0 S 0.0 0.0 0:00.21 ata_sff/0
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
[1]+ Stopped top
[root@localhost ~]#
[root@localhost ~]# top
top - 01:10:39 up 57 min, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 172 total, 1 running, 170 sleeping, 1 stopped, 0 zombie
Cpu(s): 1.5%us, 1.8%sy, 0.0%ni, 95.4%id, 0.8%wa, 0.1%hi, 0.4%si, 0.0%st
Mem: 1906188k total, 912644k used, 993544k free, 30624k buffers
Swap: 2097148k total, 0k used, 2097148k free, 521028k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
887 root 20 0 0 0 0 S 2.0 0.0 0:00.68 vmmemctl
3130 root 20 0 15024 1172 832 R 2.0 0.1 0:00.01 top
1 root 20 0 19364 1568 1240 S 0.0 0.1 0:03.52 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.14 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
7 root 20 0 0 0 0 S 0.0 0.0 0:07.77 events/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events/0
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_long/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_power_ef
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
16 root 20 0 0 0 0 S 0.0 0.0 0:00.18 sync_supers
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
19 root 20 0 0 0 0 S 0.0 0.0 0:00.89 kblockd/0
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
24 root 20 0 0 0 0 S 0.0 0.0 0:00.21 ata_sff/0
[2]+ Stopped top
[root@localhost ~]# top
top - 01:10:42 up 57 min, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 173 total, 1 running, 170 sleeping, 2 stopped, 0 zombie
Cpu(s): 1.5%us, 1.8%sy, 0.0%ni, 95.4%id, 0.8%wa, 0.1%hi, 0.4%si, 0.0%st
Mem: 1906188k total, 912948k used, 993240k free, 30624k buffers
Swap: 2097148k total, 0k used, 2097148k free, 521028k cached
Unknown command - try 'h' for help
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7 root 20 0 0 0 0 S 1.9 0.0 0:07.78 events/0
3131 root 20 0 15024 1164 832 R 1.9 0.1 0:00.02 top
1 root 20 0 19364 1568 1240 S 0.0 0.1 0:03.52 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.15 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events/0
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_long/0
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 events_power_ef
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cgroup
12 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 netns
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 async/mgr
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 pm
16 root 20 0 0 0 0 S 0.0 0.0 0:00.18 sync_supers
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 bdi-default
18 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kintegrityd/0
19 root 20 0 0 0 0 S 0.0 0.0 0:00.89 kblockd/0
20 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
21 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
24 root 20 0 0 0 0 S 0.0 0.0 0:00.21 ata_sff/0
25 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
[3]+ Stopped top
[root@localhost ~]# jobs -l
[1] 3129 停止 (信号) top
[2]- 3130 停止 (信号) top
[3]+ 3131 停止 (信号) top
注:以上示例说明Ctrl+z 快捷键可以将正在运行的进程放入后台
而且该方式放入 后台的进程会终止执行,与& 方式有所区别
以上【1】表示当前后台进程的工作号,注意与pid区别
3、将后台暂停的工作恢复到前台执行
[root@localhost ~]# fg %工作号
参数:
%工作号 :%号可以省略,但是注意工作号和PID的区别
4、将前台工作恢复到后台运行的方式
[root@localhost ~]# bg %工作号
注意:top命令必须放在前台运行,后台恢复执行的命令,是不能和前台有交互的,
否则不能恢复到后台
和前台用户有交互的命令,比如 top vi等。这些命令放到后台毫无意义。
5、如何停止后台进程?
注意,Ctrl+c等停止运行的命令,只对在前台的进程有用,
放在后台的进程是无法接受Ctrl+c 命令的,要想停止放在后台的进程,只能
先用fg 调到前台,再使用Ctrl+c等其他命令