LINUX程序后台管理

来源:互联网 发布:浙江基层网络 编辑:程序博客网 时间:2024/05/22 08:30
把一个程序从前台调到后台运行的方法:

1)command &

[root@localhost ~]# makewhatis &
[5] 9101

2)command 运行后按 ctrl+z

查看后台程序进程号和工作号命令:jobs

[root@localhost ~]# jobs
[1]- Stopped bc
[2] Stopped bc
[3]+ Stopped vi
[4] Stopped top
[5] Running makewhatis &

注意:这里有[数字] | [数字]- | [数字]+ 三种显示方式

[数字] 等待调用
[数字]- 上一次调用
[数字]+ 最近调用或者正在运行中的程序

jobs其它参数:

jobs-l :长输出格式,显示全部内容。
[root@localhost ~]# jobs -l
[工作号]调用状态 PID 运行状态 程序名
[1]- 4402 Stopped (tty output) bc
[2] 4419 Stopped bc
[3]+ 4422 Stopped (tty output) vi
[4] 4437 Stopped (signal) top
[5] 9101 Running makewhatis 

jobs-n:列出发生变化的进程ID。

jobs-p:只输出进程号。
[root@localhost ~]# jobs -p
4402
4419
4422
4437
9101

jobs-r:只输出运行的进程。
[root@localhost ~]# jobs -r
[5] Running makewhatis &

jobs -s : 只输出停止的进程。
[root@localhost ~]# jobs -s
[1]- Stopped bc
[2] Stopped bc
[3]+ Stopped vi
[4] Stopped top
[5] Running makewhatis &

如果一个程序在后台运行完后,运行的状态会显示 Done ,屏幕出现一个运行完的提示
[5] Done makewhatis &

后台程序调用命令:fg / bg
fg [工作号]

fg <<–运行调用状态是+号的程序
[root@localhost ~]# fg <<–运行调用状态是+号的程序
fg [工作号] <<– 工作号 程序
[root@localhost ~]# fg 2 <<–运行工作号为2程序
bc

bg [工作号]

bg <<– 显示调用状态是+号 程序
[root@localhost ~]# bg <<– 显示调用状态是+号 程序
[2]+ bc &

bg [工作号]  <<– 把工作号程序 调成 正在运行调用状态
[root@localhost ~]# bg 3 <<– 把工作号为3程序 调成 正在运行调用状态
[3] vi &

删除后台程序进程
kill %工作号

[root@localhost ~]# jobs
[1] Stopped bc
[2]+ Stopped bc <<–注意调用状态的变化
[3]- Stopped vi
[4] Stopped top
[root@localhost ~]# kill %2
[root@localhost ~]# jobs
[1] Stopped bc
[2]- Terminated bc <<–调用状态发生了变化,需要查看进程还在,但事实已经KILL了
[3]+ Stopped vi
[4] Stopped top
[root@localhost ~]# fg 2
bash: fg: 2: no such job
原创粉丝点击