Page 1 of 1

Backgrounding, Foregrounding and Job Control in Bash Rate Topic: -----

#1 wordswords  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 272
  • Joined: 17-December 11

Posted 01 April 2012 - 12:28 PM

Bash, the default Linux shell, has a powerful yet simple set of features for running commands in the background, and switching between commands. This set of features is known as 'job control'. Commands that you run are known as 'jobs'.

I am going to show you how to use job control on the common command 'tail -f /var/log/messages'. This command tracks new updates which have been added to the log file '/var/log/messages'. So whenever the log file '/var/log/messages' has been updated, it will print out the new lines that have been added, and it will output them directly to your terminal.

Foreground

To run a job in the foreground just run the command like you normally would:

tail -f /var/log/messages


Background

To run a job in the background, add '&' to the end of the command:

tail -f /var/log/messages &


The command will continue to run in the background, although its output will still be sent to the stdout of your current terminal, so you'll still see all the updates when they arrive, but you will be able to do other things, such as run other commands and browse around in the shell as normal.

Job list

You can output a list of running commands, both backgrounded and foregrounded jobs, by typing 'jobs'. This will show you the job number also, which is important.

Suspend

When a job is running in the foreground, ie: through running a command normally, you can press Control-Z to suspend that command. It will be paused until you do something more with it. Once you have a job suspended, you can use the following two commands: 'fg' and 'bg'.

fg sends the suspended command to the foreground, and resumes it so it executes normally
bg sends the suspended command to the background, and resumes it so it executes normally

You can also foreground or background a certain job by job number, by appending '%1' to the end of the fg or bg command, ie: 'fg %5'.

Killing

To kill a command, type 'jobs', find out the job number, and then type 'kill %' and then the job number, ie:

kill %1


That will stop the command, whether it is foregrounded or backgrounded.

Through these methods, you can have a large amount of commands running at once, and you can switch between them at will.

Is This A Good Question/Topic? 2
  • +

Replies To: Backgrounding, Foregrounding and Job Control in Bash

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Posted 04 April 2012 - 10:28 AM

Great tut, thanks!

Quote

bg – sends the suspended command to the background, and resumes it so it executes normally


One thing that I was confused on was when I would ctrl+z to suspend a job to the background, it would then be listed as "stopped". I didn't realize that I then had to type $ bg to start it going again.

This post has been edited by NotarySojac: 04 April 2012 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1