The fact that Linux is a multitasking operating system, they allow users to run multiple jobs/commands simultaneously. This is an exciting feature in Linux and Unix-like systems as users are bound to choose either to run a command on the foreground or at the background.
Firstly, let us get to know what a job control and a job means in the Linux world before digging in deep to understand how jobs are managed from the command line.
Job control is a feature of the shell which allows a single shell instance to run and manage multiple commands. Let me try to break this down. Job control is a command that allows users to run jobs either on the foreground or at the background
Then a job is a running task or command which can be running on the foreground or at the background. These jobs have a unique ID called the job number.
To understand better on the concept of a background and a foreground job in Linux, I will have to redirect your thinking to the windows operating system you are conversant with. In windows, you cannot run multiple applications at a time. It is either you send one to the background and bring one back to the foreground. Imagine working in a Microsoft word environment and trying to access your music folder, it is possible, but you can’t run both applications simultaneously. Immediately you click on another folder, it sends your current work environment to the background. You also have the option to minimize
COMMANDS USED FOR JOB CONTROL IN LINUX
jobs: The job command are used to view all jobs that are running or suspended
fg: The fg command is used to bring running or suspended job to the foreground
bg: The bg command sends jobs to the background
stop or Ctrl+ Z: The stop command or pressing the combination of control key and Z on the keyboard are used to suspend a job
kill or Ctrl +C: The kill command or pressing the combination of control key and C on the keyboard are used to terminate or kill a job
Please look up the commands above as reference when trying to run a job. We will have to take a practical example bit by bit for clear understanding.
HOW TO RUN JOBS IN THE BACKGROUND
Any command can be started in the background or by appending an ampersand (&) to the end of the command line or by pressing ctrl + z. The bash shell displays the job number and the PID of the new child process.
Let’s execute some jobs with the sleep command. First, enter the top command on your terminal and pick out the process ID of any job.
Here we have executed the sleep command to sleep three processes using their process ID. Now, they are running on the background.
To list a job, we use the job command as seen in the command above
HOW TO SEND A JOB TO THE FOREGROUND
A background job can be brought to the foreground by entering the fg command, followed by the job number.
The syntax is as follows:
fg %Job number
The above command brought job number 4 to the foreground. Note that when a job is running on the foreground, it occupies the entire terminal, preventing the user from using the terminal until it’s sent to the background.
HOW TO SEND A FOREGROUND JOB TO THE BACKGROUND
To send a foreground job to the background, you can either press ctrl+z on the keyboard, or you enter the bg command followed by the job ID of the job. The ctrl+z will suspend the job and placed it in the background as a stopped job while the bg command sends the job to the background but in a running state.
You can see from the command above, by pressing ctrl+z, I stopped the job, and by entering bg command, I placed the job in the running state.
HOW TO VIEW JOBS INFORMATION
To view information relating to a job, you use the ps j command
TIPS
Use jobs –r to list only running processes
Use jobs –s to list only suspended processes
Use jobs –l to list only process ID of the jobs
Use the kill %job number command to kill a job
When you place a stopped job either on the foreground or background, it will restart
Other shells support job control except for the Bourne shell (sh)
Just like windows operating systems, you can’t run more than one foreground job on this same terminal
You can run different jobs on different terminals
Some processes are started by the system kernel itself and cannot be brought to the foreground.
Background processes do not read or receive keyboard generated interrupts from the terminal
REFERENCES
Red Hat System Administrator I Student Workbook