Linux Commands for Jobs & ProcessesTo begin, let’s discuss processes.

We all know that a process is a series of actions, or steps, taken in order to achieve a particular end.

With that thought in mind, it should be easy to see that a computer process is a task, or a set of instructions, that is processed by a computer’s processor.

In Windows, processing happens in the background, and is rarely interactive with the average user.

In Linux, you, the average user, have the opportunity to see and control all kinds of processes.

Let’s go cause some havoc!

(Disclaimer: Quite a bit of process-controlling can be done without “messing anything up”, but as always, use some common sense to avoid wreaking havoc. The common rule-of-thumb is to leave a process alone if you don’t know what it is. That unreconisable process might just be your system’s elusive blow-up button. Or not. Because it doesn’t exist. Or does it?)

The ps Command

The ps command reports a snapshot of the current processes. It shows what processes are running.

The ps command, on it’s own, shows very little. Given various options, it can show many additional details.

The -e option selects (shows) all processes.

The -f option full-formats the process list.

The -F option “extra” full-formats the process list, which may or may not be over-kill for most needs!

The -l option shows the long format.

The -u option tidies everything up into a user-friendly format.

The -y option does not show flags, and shows rss in place of addr. It can only be used with the -l option.

As always, the options can be combined behind a single dash.

Let’s look at an example of the ps command, and break down the result to interpret each part.

Command: ps -ef

Result:

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 17:14 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 20

UID: User ID

PID: Process ID

PPID: Parent Process ID

C: CPU/Processor Usage (Percentage)

STIME: System Time Process Started

TTY: Controlling Terminal

TIME: Cumulative CPU/Processor Time

CMD: Process/Command With Arguments

The PID (process id) and CMD (process name/command details) are the details that you will no doubt pay the most attention to, at least at first.

The pstree Command

The pstree command displays a tree of processes.

It is essentially a list of processes, in a tree format, with fewer options than the ps command.

It is rather interesting to look over, a time or two.

The top Command

The top command provides a dynamic, real-time view of the running system processes.

Type in the top command, then sit and watch a moment; you should see changes happening before your very eyes.

The Up/Down arrow keys and Page Up/Page Down keys can all be used to move through the processes.

Type q to exit, or h for help with additional interactive commands.

The jobs Command

The jobs command lists jobs which are internal to the shell, and not system processes.

Each job is given a job number in braces [] that can be used as a reference number.

The -l option lists process IDs in addition to the normal information.

The -n option displays information only about jobs that have changed status since the last notification.

The -p option lists only the process ID of the job’s process group leader.

The -r option displays only running jobs.

The -s option displays only stopped jobs.

The fg & bg Commands

The pun is intended. And in case I’m being too vague, note the use of an ampersand in this sections title…

You may remember a previous mention about using ampersands in the command line. When appended to a job, the ampersand instructs that job to run in the background, freeing up the command line for continued use. Example: kate &

The fg command brings jobs to the foreground, and the bg command sends suspended jobs to the background.

Both the fg and bg commands will work on the last job started or handled, unless a job is specifically named by reference id. Example: fg %1 or bg %2

Keyboard Shortcuts That Control Processes

The Ctrl + C keyboard shortcut will kill a foreground process.

The Ctrl + Z keyboard shortcut will suspend a foreground process.

It is interesting to note that a suspended process is actually stopped. It is not running in the background, and so it can be sent to the background, or to the foreground, to be resumed.

The kill Command

The kill command sends signals to processes, usually with intent to terminate a process.

There are various signals that can be sent to a process. The default signal, TERM, will terminate a process. Other signals can be listed with the -l option. Example: kill -l

A -9 sends a kill signal to a specified process id. Example: kill -9 3846

Processes can be suspended and resumed by using the SIGSTOP and SIGCONT signals, respectively.

Examples: kill -SIGSTOP [pid] and kill -SIGCONT [pid]

To kill a job, the kill command can be used with a job reference number. Example: kill %1

Bonus Commands

The pgrep command will return the process id if the name or attribute used, matches an active process. Example: pgrep vlc

The pidof command will return the process id of a running program. Example: pidof vlc

The pkill command will kill a process if the name or attribute used matches an active process. Example: pkill vlc

The killall command will kill processes by name. Example: killall vlc *

The lsof command lists files currently open on the system.

Conclusion

Experimenting with these commands should quickly make you a process-control master.

Again, take care with which processes you “mess with”, especially if you are unfamiliar with them.