≡ Menu

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

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.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • yoander May 5, 2010, 9:50 am

    Nice!!!

  • stuff May 6, 2010, 10:54 pm

    very usefull !

  • Andy January 24, 2011, 8:04 pm

    Thanks, exactly what I am looking for.

  • Alex P March 11, 2011, 2:38 pm

    How to determine if a process running background or foreground?
    Especially, from inside: i.e. script going to use ‘read var <temr' from a terminal, but it is bad idea, if script running background; so, need to skipp reading.

  • pepe June 9, 2011, 4:18 pm

    thank you. Useful tricks ;D

  • Anonymous February 17, 2012, 12:51 am

    after executing ctrl+z and bg command on a running process, will the process resume from the point where it stopped?

  • jameslee February 23, 2012, 7:57 pm

    Excellent

  • Anonymous September 14, 2012, 4:06 pm

    how would you run the .sh script without bringing it to the foreground first?

  • Shiva Komuravelly November 16, 2012, 2:52 am

    Gud one..
    Came to know to put a foreground running process to background in between…

  • Anonymous April 9, 2013, 10:58 am

    Awesome!

  • Anonymous May 1, 2013, 1:06 am

    How can I suspend a background job?

  • Mahesh June 13, 2013, 12:51 am

    Proved to be useful for me.

    Thank You.

  • Subramani Sekar August 2, 2013, 7:56 am

    Super
    Thanks

  • Nasir September 27, 2013, 4:21 pm

    Thanks. this is really helpfull.

  • Mohamed Mansoor October 12, 2013, 11:55 am

    Nice

  • ArsenD September 10, 2014, 2:49 am

    This is EXACTLY what I was looking for, thanks! 🙂

  • Van November 19, 2014, 2:34 am

    Big thanks to you

  • Eric February 12, 2015, 6:25 am

    Is there a way to make [ctrl-z] not automatically stop a job, but instead just move it to the background?

  • Anonymous May 13, 2015, 3:45 pm

    Awesome and useful!

  • Shiva December 21, 2015, 12:36 pm

    Very useful and easy to understand

  • Anonymous January 2, 2016, 11:55 pm

    Thanks , very userful

  • Yunier January 18, 2016, 3:04 pm

    Thanks, quite useful.

  • Sai Kiran Pudi May 20, 2016, 12:22 am

    Excellent!!!

  • Ram June 27, 2017, 3:46 am

    seems screen command not available ..

    could you provide real time example for screen command