Linux工作管理方法

一 把进程放入后台
1、第一种方法
tar -zcf etc.tar.gz /etc &
top &
把命令放入后台,并在后台执行
2、第二种方法
top
按下ctrl+z快捷键,放在后台暂停
 
二 查看后台的工作
jobs [-l]
选项:
-l:显示工作的PID
注:
“+”号代表最近一个放入后台的工作,也就是工作恢复时,默认恢复的工作。
“-”号代表倒数第二个放入后台的工作。
 
三 实战
1、与用户有交互的命令放入后台,会变成stop
[root@localhost ~]# top
top - 13:56:05 up 4:54, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 152 total, 2 running, 150 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016860 total, 342592 free, 317664 used, 356604 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 515716 avail Mem
 
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8955 root 20 0 146128 2012 1420 R 0.7 0.2 0:00.39 top
10 root 20 0 0 0 0 S 0.3 0.0 0:03.12 rcu_sched
11 root 20 0 0 0 0 R 0.3 0.0 0:03.17 rcuos/0
1 root 20 0 126580 7372 2620 S 0.0 0.7 0:08.53 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.62 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
[1]+ Stopped top
[root@localhost ~]# top &
[2] 8965
[root@localhost ~]# jobs -l
[1]- 8955 Stopped (signal) top
[2]+ 8965 Stopped (signal) top
[root@localhost ~]# vi abc &
[3] 9121
[root@localhost ~]# jobs
[1] Stopped top
[2]- Stopped top
[3]+ Stopped vim abc
 
2、查看后台运行的进程
[root@localhost ~]# find / -name abc &
[4] 9150
[root@localhost ~]# jobs
[1] Stopped top
[2]- Stopped top
[3]+ Stopped vim abc
[4] Running find / -name abc &
 
四 将后台暂停的工作恢复到前台执行
fg %工作号
参数:
%工作号:%号可以省略,但注意工作号和PID的区别
 
五 把后台暂停的工作恢复到后台执行
bg %工作号
注意:后台恢复执行的命令,是不能和前台有交互的,否则不能恢复到后台执行。
 
六 实战
[root@localhost ~]# jobs -l
[1] 8955 Stopped (signal) top
[2]- 8965 Stopped (signal) top
[3]+ 9121 Stopped (tty output) vim abc
[root@localhost ~]# bg 1
[1] top &
localhost ~]#
 
 
[1]+ Stopped top
[root@localhost ~]# jobs
[1]+ Stopped top
top - 14:18:12 up 5:16, 2 users, load average: 0.00, 0.04, 0.06
Tasks: 154 total, 2 running, 150 sleeping, 2 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1016860 total, 103060 free, 343720 used, 570080 buff/cache
KiB Swap: 4194300 total, 4194300 free, 0 used. 430616 avail Mem
 
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
8955 root 20 0 146128 2012 1420 R 1.0 0.2 0:00.55 top
11 root 20 0 0 0 0 R 0.3 0.0 0:03.45 rcuos/0
1 root 20 0 126580 7372 2620 S 0.0 0.7 0:09.12 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.63 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
[root@localhost ~]# jobs
[2]- Stopped top
[3]+ Stopped vim abc
[root@localhost ~]# fg
vim abc
[root@localhost ~]# jobs
[2]+ Stopped top
[root@localhost ~]# fg
top
 
top: failed tty set: Interrupted system call.
[root@localhost ~]# jobs

猜你喜欢

转载自cakin24.iteye.com/blog/2394055