What is job control in linux

Job Control

In the previous lesson, we looked at some of the implications of Linux being a multi-user operating system. In this lesson, we will examine the multitasking nature of Linux, and how it is controlled with the command line interface.

As with any multitasking operating system, Linux executes multiple, simultaneous processes. Well, they appear simultaneous, anyway. Actually, a single processor core can only execute one process at a time but the Linux kernel manages to give each process its turn at the processor and each appears to be running at the same time.

There are several commands that are used to control processes. They are:

  • ps — list the processes running on the system
  • kill — send a signal to one or more processes (usually to «kill» a process)
  • jobs — an alternate way of listing your own processes
  • bg — put a process in the background
  • fg — put a process in the foreground

A Practical Example

While it may seem that this subject is rather obscure, it can be very practical for the average user who mostly works with the graphical user interface. Though it might not be apparent, most (if not all) graphical programs can be launched from the command line. Here’s an example: there is a small program supplied with the X Window system called xload which displays a graph representing system load. We can execute this program by typing the following:

Notice that the small xload window appears and begins to display the system load graph. On systems where xload is not available, try gedit instead. Notice also that our prompt did not reappear after the program launched. The shell is waiting for the program to finish before control returns. If we close the xload window, the xload program terminates and the prompt returns.

Putting a Program into the Background

Now, in order to make life a little easier, we are going to launch the xload program again, but this time we will put it in the background so that the prompt will return. To do this, we execute xload like this:

In this case, the prompt returned because the process was put in the background.

Now imagine that we forgot to use the «&» symbol to put the program into the background. There is still hope. We can type Ctrl-z and the process will be suspended. We can verify this by seeing that the program’s window is frozen. The process still exists, but is idle. To resume the process in the background, type the bg command (short for background). Here is an example:

Listing Running Processes

Now that we have a process in the background, it would be helpful to display a list of the processes we have launched. To do this, we can use either the jobs command or the more powerful ps command.

Killing a Process

Suppose that we have a program that becomes unresponsive; how do we get rid of it? We use the kill command, of course. Let’s try this out on xload . First, we need to identify the process we want to kill. We can use either jobs or ps , to do this. If we use jobs we will get back a job number. With ps , we are given a process id (PID). We will do it both ways:

A Little More About kill

While the kill command is used to «kill» processes, its real purpose is to send signals to processes. Most of the time the signal is intended to tell the process to go away, but there is more to it than that. Programs (if they are properly written) listen for signals from the operating system and respond to them, most often to allow some graceful method of terminating. For example, a text editor might listen for any signal that indicates that the user is logging off, or that the computer is shutting down. When it receives this signal, it could save the work in progress before it exits. The kill command can send a variety of signals to processes. Typing:

Читайте также:  Архивация windows 10 образ системы

will print a list of the signals it supports. Many are rather obscure, but several are handy to know:

Signal # Name Description
1 SIGHUP Hang up signal. Programs can listen for this signal and act upon it. This signal is sent to processes running in a terminal when you close the terminal.
2 SIGINT Interrupt signal. This signal is given to processes to interrupt them. Programs can process this signal and act upon it. We can also issue this signal directly by typing Ctrl-c in the terminal window where the program is running.
15 SIGTERM Termination signal. This signal is given to processes to terminate them. Again, programs can process this signal and act upon it. This is the default signal sent by the kill command if no signal is specified.
9 SIGKILL Kill signal. This signal causes the immediate termination of the process by the Linux kernel. Programs cannot listen for this signal.

Now let’s suppose that we have a program that is hopelessly hung and we want to get rid of it. Here’s what we do:

  1. Use the ps command to get the process id (PID) of the process we want to terminate.
  2. Issue a kill command for that PID.
  3. If the process refuses to terminate (i.e., it is ignoring the signal), send increasingly harsh signals until it does terminate.

In the example above we used the ps command with the x option to list all of our processes (even those not launched from the current terminal). In addition, we piped the output of the ps command into grep to list only list the program we are interested in. Next, we used kill to issue a SIGTERM signal to the troublesome program. In actual practice, it is more common to do it in the following way since the default signal sent by kill is SIGTERM and kill can also use the signal number instead of the signal name:

Then, if the process does not terminate, force it with the SIGKILL signal:

That’s It!

This concludes the «Learning the Shell» series of lessons. In the next series, «Writing Shell Scripts,» we will look at how to automate tasks with the shell.

Further Reading

  • For a more in-depth treatment of the topic, see Chapter 10 in The Linux Command Line.
  • 1963 Timesharing: A Solution to Computer Bottlenecks, a fascinating YouTube video from the Computer History Museum describing the first timesharing operating system and how it works. It’s basically the same method used by all modern computers.

© 2000-2021, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

Linux® is a registered trademark of Linus Torvalds.

Источник

Job Control in Linux

This is a short tutorial on job control in Linux and Unix.

What are jobs ?

In Linux or Unix, a job is defined as a task or command that has started running but not yet finished what it is doing.

As Linux and Unix are multitasking operating systems, they allow you to run multiple commands simultaneously.

Each running command is called a job, and is assigned a unique id called the job number.

It is very easy to manage jobs in Linux.

The main commands you use for job control in Linux are as follows.

  • jobs — List all the jobs that are running or suspended.
  • fg — Bring the job to the foreground.
  • bg — Send the job to the background.
  • stop or Ctrl + z — Suspend the job.
  • kill or Ctrl + c — Terminate the job.

Unique identifiers for a job

You can point out a particular job using special identifiers. They are as follows.

%n — Job whose job number / job ID is the number n.

%word — Job whose command line starts with string word.

%?word — Job whose command line contains the string word.

%+ — Current job. You can also use %% .

Читайте также:  Работа с беспроводными сетями windows

Managing jobs in Linux

How to manage jobs in Linux can be easily understood via an example.

Lets simultaneously run a few commands that takes some time to complete.

Here we have executed 4 sleep commands and all are started in the background as denoted by & .

List the running jobs

And the output is .

In the output above, the number within [ and ] is the job number (job ID). The job number is unique for each job.

Bring a job to the foreground

To bring job number 2 to the foreground, you run the following command.

Suspend the job

To suspend a job, you first bring the job to the foreground and then press the keys Ctrl + z .

Alternately, you can also suspend a job running in the background as follows.

Send a job to the background

To resume the suspended job 2 in the background, you can run the following command.

List only running jobs

List only suspended jobs

List process IDs of the jobs

This command will list process IDs of the jobs in addition to the normal information.

Terminate a job

To terminate a job, you first bring the running job to the foreground, and then press the keys Ctrl + c .

You can also terminate a job without bringing it in the foreground just by passing the job number to kill .

For example, to kill the 3rd job, you can do as follows.

Источник

Understanding the job control commands in Linux – bg, fg and CTRL+Z

Whats a job in Linux

A job is a process that the shell manages. Each job is assigned a sequential job ID. Because a job is a process, each job has an associated PID. There are three types of job statuses:
1. Foreground: When you enter a command in a terminal window, the command occupies that terminal window until it completes. This is a foreground job.
2. Background: When you enter an ampersand (&) symbol at the end of a command line, the command runs without occupying the terminal window. The shell prompt is displayed immediately after you press Return. This is an example of a background job.
3. Stopped: If you press Control + Z for a foreground job, or enter the stop command for a background job, the job stops. This job is called a stopped job.

Job Control Commands

Job control commands enable you to place jobs in the foreground or background, and to start or stop jobs. The table describes the job control commands.

Option Description
jobs Lists all jobs
bg %n Places the current or specified job in the background, where n is the job ID
fg %n Brings the current or specified job into the foreground, where n is the job ID
Control-Z Stops the foreground job and places it in the background as a stopped job

Running a Job in the Background

To run a job in the background, you need to enter the command that you want to run, followed by an ampersand (&) symbol at the end of the command line. For example, run the sleep command in the background.

The shell returns the job ID, in brackets, that it assigns to the command and the associated PID. With the job ID, you can use the job control commands to manage the job whereas the kernel uses PIDs to manage jobs.

When a background job is complete and you press Return, the shell displays a message indicating the job is done.

Managing the background jobs

You can use the jobs command to list the jobs that are currently running or suspended in the background.

You can use the fg command to bring a background job to the foreground.

You can use the ‘Control+Z keys and bg command to return a job to the background. The Control+Z keys suspend the job, and place it in the background as a stopped job. The bg command runs the job in the background. For example:
1. Using CTRL+Z

Источник

How to Use Jobs Command in Linux

The jobs command in Linux allows the user to directly interact with processes in the current shell.

Jobs have three possible states in Linux: foreground, background, and stopped.

What does that mean? I’ll explain more in a bit. First let’s look at some of the tools built into the Jobs command.

Job Control has several different functions. Each can be run independently. Here is a table of the commands related to Job Control.

Command Description
jobs Lists jobs in current shell
bg %n Send process to background
fg %n Bring process to foreground

In the table, n represents the number of the corresponding job ID.

Understanding job control in Linux

What exactly is a job anyway? Jobs, processes, and tasks are words that are often used interchangeably.

There are actually some small differences that are important for clarity.

The difference can be boiled down to which entity is handling the tasks. Users own jobs, while the kernel owns processes.

In Linux, a job can be a single process or it may have many childs or sub-processes.

An example of this may be a job that features several piped commands like this:

It parese the output of the file and sorts for uniq value using the combination of sort and uniq commands.

In personal computing, the operating system handles most processes automatically. This is usually optimized for the needs of the system or the priority given to a certain task. These processes are completed by the kernel and are given a process ID (PID).

How to control jobs in Linux

When you open a new shell, you have the power to create your own jobs. In fact, this is what happens when you enter any command (or series of commands) into a terminal.

At the user level –from a shell, you can manually send jobs to work in the background, bring them to the foreground, or suspend them (using Ctrl+z).

So, let’s put what you have learned so far into practice with a few examples.

Example 1: Send Jobs to Background

Sending a job to work in the background is easy. You just need to append your command with an & (ampersand) symbol.

You can see this command returns the job number in the brackets. The following number is the process ID used by the kernel.

Example 2: View Jobs

If you tried to type the jobs command, you might be confused that nothing seemed to happen. This is because there were no active jobs in the shell. Now that you have a job running, you can run the jobs command successfully.

The PID number isn’t listed here, but you don’t need it. You control jobs using the user/shell-specific job ID.

Example 3: Bring Jobs to Foreground

You can use that Job ID to bring your instance of nano to the foreground.

If you have been following along, this should bring you into Nano with your file named Linux Handbook.

Example 4: Stop a Job with Ctrl+z

You can stop open jobs by using Ctrl+z. Here’s a screenshot that shows how to use this with top.

In the screencast, you can watch me open top, use Ctrl+z to suspend it, view it in my jobs list, and then bring it back to the foreground before closing it.

Example 5: Run Background Jobs Using bg

While you can send jobs to the background with the & symbol, it’s always good to have an alternative. In the screencast, you watched me stop the job using ctrl+z. I brought the job back to the shell and the foreground with the fg %n command. You can do the same thing using bg.

I am going to use a GUI application just to switch things up. You may have noticed in the past that when you open a GUI from the terminal that if you close the terminal, the GUI will close too. Hopefully, this article has clarified for you why that happens.

So, let’s use Gedit. I’m going to do the following:

  • Start Gedit
  • Return to the Shell and Ctrl+z
  • Send it to the Background to Run using bg
  • Check my Jobs List to Verify it is Running
  • Return to Foreground and Send a system interrupt using Ctrl+c to Terminate it

Bonus Tip on termination jobs in Linux

Many of us are used to using ctrl+c to terminate jobs in a shell. There is another way to do this that incorporating your new job control knowledge.

That’s right, the kill command can be used along with the job id to terminate a job also.

Conclusion

Learning the Linux command line brings us closer to the inner-workings of our favorite operating system. Learning jobs control gives us the ability to handle multiple jobs at once.

I hope that you found something new to explore with this article. If you enjoyed it, please let us know in the comments section below or use the links to share on social media.

Источник

Читайте также:  Windows named pipe name
Оцените статью