---
title: "我使用过的Linux命令之jobs - 显示当前终端关联的后台任务情况"
author: "codingstandards"
date: "2010-10-10T18:36:37Z"
tags: ["Blog", "F#", "Linux", "SUN"]
categories: ["操作系统"]
slug: "linux-jobs"
draft: false
lastmod: "2011-04-10T11:28:30Z"
id: 780581
url: "https://codingstandards.iteye.com/blog/780581"
---

# 我使用过的Linux命令之jobs - 显示当前终端关联的后台任务情况

本文链接：[http://codingstandards.iteye.com/blog/780581](/blog/780581)    （转载请注明出处）

 

## 用途说明

jobs命令用于显示当前终端关联的后台任务情况。

命令后面跟上& 用于将命令在后台执行。

Ctrl+Z用于将当前正在运行的前台进程暂停，变成后台进程。

bg \[%n]用于将后台暂停的进程继续运行。

fg \[%n]用于将后台执行的进程变成前台进程。

kill %n用于杀掉指定的任务。

 

## 常用参数

-l      显示进程组ID和作业在运行的目录。  
\-n     只显示上次显示过的已经停止的或已经退出的作业。  
\-p     只显示选定作业的进程组的进程ID.

## 使用示例

### 示例一

\[root@jfht ~]# jobs  
\[root@jfht ~]# tail -f job.sh  
#!/bin/sh

date &gt;job.txt

Ctrl+Z  
\[1]+  Stopped                 tail -f job.sh  
\[root@jfht ~]# jobs -l  
\[1]+  3034 停止                    tail -f job.sh  
\[root@jfht ~]# bg  
\[1]+ tail -f job.sh &  
\[root@jfht ~]# jobs -l  
\[1]+  3034 Running                 tail -f job.sh &  
\[root@jfht ~]# kill %1  
\[root@jfht ~]# jobs -l  
\[1]+  3034 已终止                  tail -f job.sh  
\[root@jfht ~]# jobs -l  
\[root@jfht ~]# tail -f job.sh  
#!/bin/sh

date &gt;job.txt

\[1]+  Stopped                 tail -f job.sh  
\[root@jfht ~]# jobs -l  
\[1]+  3306 停止                    tail -f job.sh  
\[root@jfht ~]# jobs  
\[1]+  Stopped                 tail -f job.sh  
\[root@jfht ~]# fg  
tail -f job.sh

Ctrl+C  
\[root@jfht ~]# jobs  
\[root@jfht ~]#

### 示例二

\[root@jfht ~]# tail -f job.sh  
#!/bin/sh

date &gt;job.txt

\[2]+  Stopped                 tail -f job.sh  
\[root@jfht ~]# bg  
\[2]+ tail -f job.sh &  
\[root@jfht ~]# exit  
logout  
**There are stopped jobs.**  
\[root@jfht ~]# exit  
logout

 

重新连接并登录。

Last login: Sun Oct 10 16:54:10 2010 from 222.70.154.57  
\[root@jfht ~]# ps -ef|grep tail  
root      6464     1  0 18:40 ?        00:00:00 tail -f job.sh  
root      6579  6550  0 18:41 pts/8    00:00:00 grep tail

\[root@jfht ~]# killall tail  
\[root@jfht ~]# killall tail  
tail: no process killed

 

## 问题思考

1\. 有哪些方式可以让命令后台执行？

2\. 前台进程与后台进程有哪些区别？

## 相关资料

【1】罗明的博客 [Linux下使用Shell命令控制任务（Jobs）执行](http://www.blogjava.net/Andyluo/archive/2005/10/31/17572.html)

【2】linux [fg bg ctrl + z jobs & 等命令](http://blog.chinaunix.net/u2/67555/showart_1110905.html)

 

返回 [我使用过的Linux命令系列总目录](/blog/786653)