Saturday, September 19, 2015

Linux Bash Shell Tutorial: How to manage background and foreground jobs in Linux terminal.




In this post, I'm going to share on how to manage background and foreground jobs in Linux terminal. Before I go into details, lets look at some  jobs basic terminology .

Jobs are processes that running in a particular shell .Many  jobs can run in the shell at once.Only one job can be active in the terminal at once .However  many jobs can be executed in the background which known as Background Job.The active job in the current terminal is call Foreground Job

Tutorial Content List

Running jobs in background .
Listing Jobs: Listing your jobs.
Suspending Jobs: Suspending a job to do something else.
Background Jobs: Having jobs work in the background.
Foreground Jobs: Putting jobs in the foreground.
Terminating Jobs: Stopping jobs before they're finished.


1)Running jobs in the background .

To run command in the background run 



Running jobs in the background

Once you run the job in the background, the terminal will assign a job id and a process number respectively. job id is the number in the bracket[] followed with process number


2)Listing Jobs.


run "jobs"  command to check the jobs status.



jobs status


NOTE:  the plus(+) sign shows  which is the latest job executed in the terminal and the minus(-) sign shows the second latest job that was executed in the terminal.you can access the specific job by referring the job  id .


3)Suspending Jobs.

To suspend active job in the terminal, press "CTRL-Z"


CTRL-Z

Once suspended, the job status will show  as "stopped" .  Now you you can choose to run the job either in the foreground or in terminal background.

4)Background Jobs .

To put the recent executed job in the background, run "bg" command. This command will put the last executed job in the background  (in other words the job with the plus(+) sign .


Background jobs
If you want to run specific job id in the background, use "bg" command followed with  "%"  and the job id number .



Running job id 1 in the background
5)Foreground Jobs.

To put the recent executed job in the foreground, run "fg" command. This command will put the last executed job in foreground  (in other words the job with the plus(+) sign .


Foreground jobs


On executing the command, job with id 3 is shown in the terminal as it was the latest job executed in the terminal.

to put a specific job id on the terminal, use "fg" command followed with  "%" and the job id . 



specific job id in the foreground


5)Terminating Jobs.
To stop jobs before they finish executing,  press "CTRL -C"

CTRL-C

Or you can also use "kill" command to terminate the job



kill the job












Read Previous : Linux Bash Shell Tutoria : How to use pipeline and filters in Linux Terminal. 8 Tips and Tricks

No comments:

Post a Comment