What Is a Process in Linux?

The term process is often unfamiliar to anyone without a Computer Science background. However, it’s one that is often used when discussing Linux programming, and processes are essential to system administration work.

Linux also uses the term job to refer to a very similar concept. The difference is subtle but important, and job control is a useful tool when running a multi-tasking environment. You can use a number of tools and built-in commands to juggle jobs.

What Is a Process?

At the very simplest level, you can think of a process as the equivalent of a program that you run. It may be a full-blown GUI application such as your web browser. It could be a single one-off command you run on the command line, such as ls. Broadly speaking, anything that happens on your computer depends on a process, at its heart.

In reality, a single application can utilize many processes to carry out separate tasks simultaneously. A command-line invocation using pipes, such as:

$ grep "error" log.txt | wc -l

Will execute two separate processes, one for each pipe segment.

How Processes Begin

Processes are either created explicitly by you, the user, or automatically by your computer itself. In fact, you may have hundreds of processes already running as soon as you’ve booted up.

Processes can spawn other processes and init, the first process that starts on many traditional Linux systems, is ultimately responsible for starting every process that runs.

How Processes End

Many processes are short-lived commands which carry out a task and then stop. Typing ls into a terminal will start, execute, and stop a process within fractions of a second.

Some processes, such as daemons, run continuously. The cron process, for example, executes other commands periodically whilst its host computer is running.

Identifying a Process

The Operating System (OS) assigns a unique identifier to every process. It’s known as the PID or process ID. This value is typically a 1-5 digit number and future processes can reuse the PID of a previous process that has been fully cleaned up.

PIDs are used by the OS itself in many different ways. A good example is the /proc directory which stores information about currently running processes.

Related: What Are Those Folders in Your Linux Root Directory?

What Is a Job?

In Linux terminology, a job is a program managed by the shell. It typically consists of one process, but may use several. When you enter a command in your terminal, a process is spawned to execute the command, and a job is created to help control the command whilst it’s running.

Managing Jobs

If you’re running a job in the foreground, you can interrupt it by pressing Control+C (^C). Typically, this will cause the process to quit and will return the terminal to a prompt.

$ sleep 100
^C
$

Alternatively, pressing Control+Z (^Z) will stop the job from running, but not cause it to end. You can think of it more like a pause.

$ sleep 100
^Z
[1]+ Stopped sleep 100
$

Note that the shell tells you the number of the job in square brackets when you stop it. This can be used with other commands to control the job. For example, you can restart a job by bringing it to the foreground using fg:

$ fg %1
sleep 100

You can use a similar command to restart the job in the background:

$ bg %1
[1]+ sleep 100 &
$

This will return control to a prompt, so you can carry on with other work whilst the job runs. If you want a job to run in the background as soon as you start it, add an & to the end of the command:

$ sleep 100 &
[1] 61087
$

In this case, the shell prints the job number in brackets and the PID afterward.

Common Tools for Interrogating Processes and Jobs

Monitoring Processes

One of the most useful commands to get information about processes is top. The program shows a real-time, interactive view of running processes. It’s the command-line equivalent of graphical programs such as GNOME’s System Monitor or the Windows Task Manager.

You can start the top program with the simple command, top:

A header area displays CPU load and memory usage. Below this, top shows a table containing one process per line. Details include the PID, how much available CPU power the process is using, and the total CPU time it has consumed. Information is refreshed automatically, every three seconds by default.

There are many options and interactive commands that can be used to alter top’s behavior. Use the man top command to read more:

Getting a Snapshot of Active Processes

Short for process status, the ps command lists processes. Different options allow for various filtering and adjustments to the details displayed. By default, ps shows processes that are attached to a terminal and were started by the current user. In other words, mostly commands you have typed onto the command line.

With the earlier task still backgrounded, output might look a little like this:

$ ps
PID TTY TIME CMD
35564 ttys000 0:00.00 sleep 100
73998 ttys000 0:00.43 -bash

As with top, ps has many options to control its behavior, and these can be discovered via man ps:

Two of the most useful, which are often combined, are -e and -f. They show processes owned by all users, and additional columns respectively. For example:

$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2020 ? 00:11:22 /sbin/init
root 2 0 0 2020 ? 00:00:00 [kthreadd]
...

Listing Background Jobs

The jobs command lists background jobs in the current shell. To demonstrate its use, start a long-running job in the background:

$ du -skh ~ >/tmp/du.txt 2>/dev/null &
[1] 61167

This command calculates the total disk space used by your home directory, redirecting its output to a temporary file.

$ jobs
[1]+ Running du -skh ~ > /tmp/du.txt 2> /dev/null &

Eventually, when the job completes, a line will appear in your terminal similar to:

[1]+ Exit 1 du -skh ~ > /tmp/du.txt 2> /dev/null

Sending Signals to Terminate Processes

If you’ve identified a misbehaving process, you might need to kill it. Although it sounds drastic, the kill command is a normal part of a system administrator’s toolbox. It can send any one of several signals, which are standard notifications to control process behavior. Some common signals to send are SIGINT, SIGTSTP, SIGTERM, and SIGKILL.

SIGINT is the equivalent of pressing ^C. SIGTSTP is the equivalent of pressing ^Z. SIGTERM and SIGKILL are both means of stopping a process. The former sends a request to the process, giving it a chance to shut itself down gracefully. The latter is a more extreme method of forcing a process to quit and should be used as a last resort.

The kill command can also work with jobs. For example:

$ jobs
[1]+ Running sleep 100 &
$ kill %1
[1]+ Terminated: 15 sleep 100

Read more: Ways to Kill Unresponsive Programs in Linux

Working With Processes and Jobs in Linux

Processes and jobs are tricky concepts to grasp, in particular the difference between them. However, they are one of the first steps towards understanding system administration under Linux. Jobs are a practical means of running different commands from the shell simultaneously.

Processes are a lower-level concept that can also be manipulated and are at the heart of every program that runs on a computer.

Source: makeuseof.com

Related posts

How to Clear Your Browser History on Chrome, Firefox, Brave, and More

Connections #328: Today’s Answer and Clues (Saturday, May 4, 2024)

These 5 iPhone Features Helped Me Minimize Distractions at Work