Process Management in a Terminal

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
377
Credits
11,920
Within a terminal you can start a process, such as updating your system packages. But what if you want to perform more tasks rather than wait for the update to finish?

We can place processes in the background and let them continue to run while we run other processes in the foreground. It is possible to cancel paused processes. If needed, we can put the process back in the foreground.

If needed, we can have multiple processes running at the same time in one terminal.

Listing Processes

Within a terminal, we can create multiple processes, but we need to monitor what is running.

Open a terminal and use the command 'ps'. If you just opened the terminal, then you should get something back like in Figure 1.

Figure 1.JPG

FIGURE 1

NOTE:
Any Process IDs will be different between the figures and what is displayed on your system.

The first column lists the Process ID (PID) of the process. Each process has its own unique ID, so we can manage it. The second column is for the terminal that is a Pseudo Terminal Service (pts) that it attached the process to, which is TTY 0 in this case. If you open multiple terminals, each will have a number incremented by 1, but starting at 0. In the third column is the time that the specific command has used of the processor. The last column is the command. Of course, one is 'ps' that we executed to see the output. The other one is 'bash', but only if your shell is BASH.

Another command we can use to see any processes that are in the background or paused is 'jobs'. If you run it now, it will show no running jobs. We'll create one soon to use the command and see how it works.

Once we know what processes are running, we can manage them better. We can purposefully place a process in the background.

Starting a Process in the Background

The terminal includes a few commands to help us manage the processes. Let's start with something easy.

If we want to run a command and place it in the background, then we need to use the ampersand at the end of the command. For example, let's use the sleep command to hold a process for one minute. The command would be 'sleep 1m', but if we run it as is, the process will hold the terminal up for one minute. So, run the command 'sleep 1m &'. We ran the command in the background and you can the run the command 'jobs' to see the running process. We can see this in Figure 2.

Figure 2.JPG

FIGURE 2

After we ran the command, we get the line '[1] 30918'. The first number, in brackets, is the Job Number. The second number is the Process ID (PID) for the child process of the 'sleep' command.

Keep in mind that the terminal is the parent process. Any jobs you start within the terminal are child processes. If a child process is running and you terminate the parent, all child processes end as well. We can look into fixing that later.

When running 'jobs' you only get a listing of the Job Number and its status.

After the close bracket, is the possibility of three values. Here, the value is a plus sign. The plus sign shows that this is the 'current' process, or the most recent one. The minus sign is the previous one to the current process. Any that are blank are those that are not the most current or previous processes. If you run the command 'sleep 1m &' three times, then run 'jobs', you'll see all three values. We show these in Figure 3. Since Job 3 ran last, it will be the current process, and Job 2 is the previous Job.

Figure 3.JPG

FIGURE 3

We now know how to start a process in the background, but what if a job is in the foreground?

Moving a Job to the Background or Foreground

If a job is in the foreground, we can end the job by pressing CTRL+C. When you start a process, like 'sleep 10s', you can press CTRL+C to stop it. If you run 'ps' or 'jobs', you'll see that it is no longer running. But we need to stop the process and leave it paused in memory. To pause a process, use CTRL+Z.

Run the command 'sleep 10s' and then press CTRL+Z. You should then see a line showing the Job Number, followed by a '+'. The status of 'Stopped' and the command itself. If you now run 'ps' or 'jobs', you'll see the process on the list. If you wait longer than 10 seconds, the time to sleep, you'll see that the process is still on the list. It cannot end because it is paused and not running to continue to countdown the time given it.

You can place the job in the background to continue running with the command 'bg 1'. The '1' is the Job Number. You can also place the job in the foreground with 'fg 1'. Again, the number is the Job Number.

Once resumed in the foreground or background, the stopped process will continue.

Instead of specifying a job number, you can specify the Job Number preceded by a percent sign. If the job is the most current, you can use a plus sign. We can signify the second most recent with a minus sign.

If we stop a process, what do we do to terminate the process?

Killing a Process

If a process is in the Foreground, we can terminate the process with CTRL+C. But what if the process is in the Background already?

We could move it to the foreground and then CTRL+C, but this may not be suitable. There are other ways.

Closing the terminal will terminate all child processes, but we may have another process in the background that we need to complete.

By running 'ps' you can find the PID for the process you want to terminate. Once you have the PID for the process, you can run 'kill <PID>'. If you look back at Figure 2, there was a process with an ID of '30918'. We can use the command 'kill 30918'. By default, this uses the parameter value of '-15' for 'SIGTERM'. When a script or program receives the 'SIGTERM' signal, it can gracefully exit. Any error handling managed by the process will be executed. The process may respond with the message 'Terminated'.

You can also pass a different signal, which is best for processes that aren't responding at all. Use the parameter '-9' to instantly terminate the process. Using the 'SIGKILL' signal will cause the process to terminate immediately and not perform any error handling. The system may respond with a 'Killed' message. Remember to always specify the PID number. You may not want to use the '-9'parameter for a service that needs to handle some type of cleanup processes if it needs to exit.

I should note that not all kill parameters will terminate a process. If a process is in the background, you can cause it to 'stop' or pause with the command 'kill -19 <PID>'. If you run 'ps' or 'jobs', you'll see the process has been stopped. Using the command 'kill -18 <PID>', you can resume the process where it was paused. The 'kill -18 <PID> is the same as a 'bg' command.

Multitasking

You can place multiple processes into the background and leave them running. Do not stop them so they can complete. By using the 'ps' and 'jobs' command, you can monitor the processes.

By leaving multiple processes running in the background, they are multitasking within the terminal. If you should close the terminal, the child processes will all end.

You should be able to accomplish getting more done from one terminal than opening a terminal for each process.

Changing a child Process to a Parent Process

If you have a running child process in a terminal, you can 'disown' the process with the command 'disown -h <PID>'.

Once executed, the process is no longer a child process of the terminal and the terminal can be closed without affecting the process.

Once the terminal is closed, you can look at the process from another terminal with the command 'ps <PID>'. The TTY listed should now be a question mark showing that it is no longer owned by a parent process.

The process will run to completion and end on its own, or it will run until someone kills the process or reboots the system.

Once disowned, it detached the process from all terminals. You can use 'ps <PID>' to check it's status, or kill it, but no other interaction can occur.

Conclusion

You can hopefully see how to manage the process within a terminal better. To move the processes from the foreground to the background, to pause, resume, and kill a process.

Instead of having multiple tabs opened for each process, you can manage them all in one terminal window.

Multitasking in a terminal window just got a lot easier.
 

Members online


Top