Bg, Fg, &, Ctrl-Z – 5 Examples to Manage Unix Background Jobs

来源:互联网 发布:小说录入软件 编辑:程序博客网 时间:2024/05/18 01:27

When you execute a unix shell-script or command that takes a long time, you can run it as a background job.

In this article, let us review how to execute a job in the background, bring a job to the foreground, view all background jobs, and kill a background job.

1. Executing a background job

Appending an ampersand ( & ) to the command runs the job in the background.

For example, when you execute a find command that might take a lot time to execute, you can put it in the background as shown below. Following example finds all the files under root file system that changed within the last 24 hours.

# find / -ctime -1 > /tmp/changed-file-list.txt &

2. Sending the current foreground job to the background using CTRL-Z and bg command

You can send an already running foreground job to background as explained below:

  • Press ‘CTRL+Z’ which will suspend the current foreground job.
  • Execute bg to make that command to execute in background.

For example, if you’ve forgot to execute a job in a background, you don’t need to kill the current job and start a new background job. Instead, suspend the current job and put it in the background as shown below.

# find / -ctime -1 > /tmp/changed-file-list.txt# [CTRL-Z][2]+  Stopped                 find / -ctime -1 > /tmp/changed-file-list.txt# bg

3. View all the background jobs using jobs command

You can list out the background jobs with the command jobs. Sample output of jobs command is

# jobs[1]   Running                 bash download-file.sh &[2]-  Running                 evolution &[3]+  Done                    nautilus .

4. Taking a job from the background to the foreground using fg command

You can bring a background job to the foreground using fg command. When executed without arguments, it will take the most recent background job to the foreground.

# fg

If you have multiple background ground jobs, and would want to bring a certain job to the foreground, execute jobs command which will show the job id and command.

In the following example, fg %1 will bring the job#1 (i.e download-file.sh) to the foreground.

# jobs[1]   Running                 bash download-file.sh &[2]-  Running                 evolution &[3]+  Done                    nautilus .# fg %1

5. Kill a specific background job using kill %

If you want to kill a specific background job use, kill %job-number. For example, to kill the job 2 use

# kill %2

To kill a foreground jobs, use one of the methods specified in our earlier article 4 Ways to Kill a Process — kill, killall, pkill, xkill.

http://www.thegeekstuff.com/2010/05/unix-background-job/

fg、bg、jobs、&、ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的

一.& 最经常被用到

这个用在一个命令的最后,可以把这个命令放到后台执行

二.ctrl + z

可以将一个正在前台执行的命令放到后台,并且暂停

三.jobs

查看当前有多少在后台运行的命令

四.fg

将后台中的命令调至前台继续运行

如果后台中有多个命令,可以用 fg %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)

五.bg

将一个在后台暂停的命令,变成继续执行

如果后台中有多个命令,可以用bg %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)

Linux下使用Shell命令控制任务Jobs执行

下列命令可以用来操纵进程任务:

ps 列出系统中正在运行的进程;

kill 发送信号给一个或多个进程(经常用来杀死一个进程);

jobs 列出当前shell环境中已启动的任务状态,若未指定jobsid,则显示所有活动的任务状态信息;如果报告了一个任务的终止(即任务的状态被标记为Terminated),shell 从当前的shell环境已知的列表中删除任务的进程标识;

bg 将进程搬到后台运行(Background);
  fg 将进程搬到前台运行(Foreground);
  将job转移到后台运行
  如果你经常在X图形下工作,你可能有这样的经历:通过终端命令运行一个GUI程序,GUI界面出来了,但是你的终端还停留在原地,你不能在shell中继续执行其他命令了,除非将GUI程序关掉。
  为了使程序执行后终端还能继续接受命令,你可以将进程移到后台运行,使用如下命令运行程序: #假设要运行xmms
  $xmms &
  这样打开xmms后,终端的提示又回来了。现在xmms在后台运行着呢;但万一你运行程序时忘记使用“&”了,又不想重新执行;你可以先使用ctrl+z挂起程序,然后敲入bg命令,这样程序就在后台继续运行了。
  概念:当前任务
  如果后台的任务号有2个,[1],[2];如果当第一个后台任务顺利执行完毕,第二个后台任务还在执行中时,当前任务便会自动变成后台任务号码“[2]”的后台任务。所以可以得出一点,即当前任务是会变动的。当用户输入“fg”、“bg”和“stop”等命令时,如果不加任何引号,则所变动的均是当前任务。
  察看jobs
  使用jobs或ps命令可以察看正在执行的jobs。
  jobs命令执行的结果,+表示是一个当前的作业,减号表是是一个当前作业之后的一个作业,jobs -l选项可显示所有任务的PID,jobs的状态可以是running, stopped, Terminated,但是如果任务被终止了(kill),shell 从当前的shell环境已知的列表中删除任务的进程标识;也就是说,jobs命令显示的是当前shell环境中所起的后台正在运行或者被挂起的任务信息;
  进程的挂起
  后台进程的挂起:
  在solaris中通过stop命令执行,通过jobs命令查看job号(假设为num),然后执行stop %num;
  在redhat中,不存在stop命令,可通过执行命令kill -stop PID,将进程挂起;
  当要重新执行当前被挂起的任务时,通过bg %num 即可将挂起的job的状态由stopped改为running,仍在后台执行;当需要改为在前台执行时,执行命令fg %num即可;
  前台进程的挂起:
  ctrl+Z;
  进程的终止
  后台进程的终止:
  方法一:
  通过jobs命令查看job号(假设为num),然后执行kill %num
  方法二:
  通过ps命令查看job的进程号(PID,假设为pid),然后执行kill pid
  前台进程的终止:
  ctrl+c
  kill的其他作用
  kill除了可以终止进程,还能给进程发送其它信号,使用kill -l 可以察看kill支持的信号。
  SIGTERM是不带参数时kill发送的信号,意思是要进程终止运行,但执行与否还得看进程是否支持。如果进程还没有终止,可以使用kill -SIGKILL pid,这是由内核来终止进程,进程不能监听这个信号。

http://blog.sina.com.cn/s/blog_673ee2b50100iywr.html


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩儿写字做作业磨蹭怎么办 孩子不学习成绩差不写作业怎么办 儿童4岁不会写字怎么办 小孩读中班还不会写字怎么办 幼儿园中班孩子不写字怎么办 小孩吃多了呕吐怎么办 一岁宝宝老便秘怎么办 4周岁半幼儿便秘怎么办 没胃口不能吃辣怎么办 九个月宝宝缺锌怎么办 宝宝4 5天不吃饭怎么办 16个月宝宝不肯吃饭怎么办 儿童不吃饭很瘦怎么办 小孩吃饭吃的少怎么办 宝宝突然晚上不睡觉怎么办 l岁宝宝吃多了怎么办 3个月婴儿厌食怎么办 11个月婴儿厌食怎么办 7个月的婴儿厌食怎么办 小孩记忆不好读书记不住怎么办 9岁儿童不爱睡觉怎么办 6岁儿童不爱吃饭怎么办 2岁半宝宝不吃饭怎么办 一年级的孩子不爱学习怎么办 小孩不爱写作业怎么办啊 孩子不爱看书怎么办如何教育 2岁宝宝不爱看书怎么办 儿媳妇比儿子年龄大我不喜欢怎么办 不喜欢儿子却生了儿子怎么办 静不下心来看书怎么办 孩子爱玩不爱学怎么办 孩子爱玩不爱学习怎么办 孩子爱玩手机不爱学习怎么办 照四维宝宝太活泼了怎么办 胎宝宝太活泼了怎么办 7个月宝宝太活泼怎么办 我是个初中生不想上学怎么办 3岁宝宝不肯说话怎么办 两周宝宝不爱吃饭怎么办 小孩不喜欢吃水果蔬菜怎么办 孩子对学习不感兴趣怎么办