Linux培训笔记:Linux任务的前台和后台执行
Linux培训笔记:Linux任务的前台和后台执行
jobs 可以查看后台正在运行的作业。
Ctrl+C 可以立即终止正在运行的任务。
Ctrl+Z 可立即将正在运行的任务转到后台暂时挂起并保留在内存中。
fg 【jodid】 回到之前挂起的任务,也可以回到之前使用&符号在后台运行的任务。
简单举例:
[root@TSMClt ~]# whoami
root
[root@TSMClt ~]# jobs
[root@TSMClt ~]#
[root@TSMClt ~]# top
top - 05:08:56 up 1 day, 3:13, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 153 total, 1 running, 152 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.1%sy, 0.0%ni, 97.5%id, 1.8%wa, 0.0%hi, 0.5%si, 0.0%st
Mem: 996752k total, 822016k used, 174736k free, 33588k buffers
Swap: 1998840k total, 333864k used, 1664976k free, 190192k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19244 1180 964 S 0.0 0.1 0:00.58 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.40 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.17 migration/1
7 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/1
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/1
9 root 20 0 0 0 0 S 0.0 0.0 0:00.55 events/0
10 root 20 0 0 0 0 S 0.0 0.0 0:01.16 events/1
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuset
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.03 sync_supers
17 root 20 0 0 0 0 S 0.0 0.0 0:00.02 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.00 kintegrityd/1
20 root 20 0 0 0 0 S 0.0 0.0 0:00.13 kblockd/0
21 root 20 0 0 0 0 S 0.0 0.0 0:01.26 kblockd/1
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
24 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
25 root 20 0 0 0 0 S 0.0 0.0 0:00.33 ata/0
26 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ata/1
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
30 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kseriod
33 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
34 root 20 0 0 0 0 S 0.0 0.0 0:02.16 kswapd0
[1]+ Stopped top
[root@TSMClt ~]#
使用Ctrl+Z,使得任务到后台挂起。
[root@TSMClt ~]#
[root@TSMClt ~]# jobs
[1]+ Stopped top
[root@TSMClt ~]#
[root@TSMClt ~]# jobs
[1]+ Stopped top
[root@TSMClt ~]# vmstat 1 500
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 333856 173124 33604 190204 1 3 17 24 129 97 0 1 98 2 0
0 0 333856 173124 33604 190204 0 0 0 0 2013 126 0 0 100 0 0
0 0 333856 173124 33604 190204 0 0 0 0 2005 117 0 0 100 0 0
^Z
使用Ctrl+Z,使得任务到后台挂起。
[2]+ Stopped vmstat 1 500
[root@TSMClt ~]# jobs
[1]- Stopped top
[2]+ Stopped vmstat 1 500
[root@TSMClt ~]#
[root@TSMClt ~]#
[root@TSMClt ~]# top &
[3] 8207
[root@TSMClt ~]#
[3]+ Stopped top
[root@TSMClt ~]# jobs
[1] Stopped top
[2]- Stopped vmstat 1 500
[3]+ Stopped top
使用fg命令可以回到之前暂挂的任务。
[root@TSMClt ~]# fg 1
top
top - 05:19:02 up 1 day, 3:23, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 154 total, 1 running, 151 sleeping, 2 stopped, 0 zombie
Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 996752k total, 823760k used, 172992k free, 33604k buffers
Swap: 1998840k total, 333856k used, 1664984k free, 190204k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8185 root 20 0 14940 1228 912 R 0.3 0.1 0:00.01 top
1 root 20 0 19244 1180 964 S 0.0 0.1 0:00.58 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.40 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.17 migration/1
7 root 20 0 0 0 0 S 0.0 0.0 0:00.11 ksoftirqd/1
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/1
9 root 20 0 0 0 0 S 0.0 0.0 0:00.55 events/0
10 root 20 0 0 0 0 S 0.0 0.0 0:01.16 events/1
11 root 20 0 0 0 0 S 0.0 0.0 0:00.00 cpuset
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.03 sync_supers
17 root 20 0 0 0 0 S 0.0 0.0 0:00.02 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.00 kintegrityd/1
20 root 20 0 0 0 0 S 0.0 0.0 0:00.13 kblockd/0
21 root 20 0 0 0 0 S 0.0 0.0 0:01.26 kblockd/1
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpid
23 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_notify
24 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kacpi_hotplug
25 root 20 0 0 0 0 S 0.0 0.0 0:00.33 ata/0
26 root 20 0 0 0 0 S 0.0 0.0 0:00.04 ata/1
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ata_aux
28 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksuspend_usbd
29 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
30 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kseriod
33 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
例子挺简单,但是挺有用,小记之。