The Linux terminal has a number of useful commands that can display running processes, kill them, and change their priority level.
Each unix process has two ID numbers assigned to it: The Process ID (pid) and the Parent process ID (ppid). Each user process in the system has a parent process.
When you start a process (run a command), there are two ways you can run it −
1. Foreground Processes
By default, every process that you start runs in the foreground. If the command is like “ls” it will print the output, and, exit the command, most likely when a persistent program runs it stays and the terminal actively shows that running, events are displayed in the screen, in other words, the terminal becomes exclusively part of the program.
2. Background Processes
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.
The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another. Meaning the terminal is no longer exclusive to that newprocess.
& = send program to background
Sending a program to foreground or background
fg = send to foreground
bg = send to background
use jobs to list the programs running for that terminal.
As we can see the program is running in the foreground
To send it to the background stop it (Ctrl + z) , and then run bg command
1. To show processes use ‘ps’, run in bash
-f = Do full-format listing.
2. Show all information of all processes running
-e = Select all processes. Identical to -A
An alternative is
3. Display process tree
4. List processes dynamically
watch -n 2 = running the ps command every 2 seconds
Display a tree of processes
Print the tree with PID
Sorting the output
-n = Sort processes with the same ancestor by PID instead of by name
Filter by processes also
-s = Show parent processes of the specified process.
Filter process by users
top command is used to show the Linux processes. It provides a dynamic real-time view of the running system.
Display processes by user
Show absolute path
While running to you can run commands
Use ‘h’ to display help menu
z = color output
k = kill a process
In this example we will kill Firefox, PID 4758
Choose the signal to send the kill
pgrep returns the process IDs that match it.
-c = Suppress normal output; instead print a count of matching processes.
Print full PID and child PID
-f = The pattern is normally only matched against the process name. When -f is set, the full command line is used.
-i = Match processes case-insensitively.
Print command line and PID
-a = List the full command line as well as the process ID.
Nice is a command in Unix and Linux operating systems that allows for the adjustment of the “Niceness” value of processes. Adjusting the “niceness” value of processes allows for setting an advised CPU priority that the kernel's scheduler will use to determine which processes get more or less CPU time.
Different OS distributions can have different default values for new processes. The simplest method to determine the default value is to simply run the nice command with no arguments.
Nice value is a user-space and priority PR is the process's actual priority that use by Linux kernel
System priorities are 0 to 139 in which 0 to 99 for real time and 100 to 139 for users
Nice value range is -20 to +19 where -20 is highest, 0 default and +19 is lowest.
Checking current nice value of a running process
Changing the nice value of a new process
The nice command itself will run the supplied command with the desired niceness value. This time the value is one, overwriting the default 0
Changing the nice value of a running process
To change the niceness value of a running process we will utilize the renice command.
Zombie and Orphan Processes
Normally, when a child process is killed, the parent process is updated via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed.
Daemons are system-related background processes that often run with the permissions of root and services requests from other processes.
A daemon has no controlling terminal. It cannot open /dev/tty. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty.
Send a signal to a process. The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL.
Kill a process
Send SIGKILL (powerful)
Killing a job
pkill kills processes based on name and other attributes
kill processes by name
List the signal
Kill a process by name
Kill using a signal
Kill processes by user
A process may not continue to run when you log out or close your terminal. This special case can be avoided by preceding the command you want to run with the nohup command.
Proc file system (procfs) is virtual file system built at run time. It contains the useful information about the processes that are currently running, it is regarded as control and information centre for kernel.
The proc file system also provides communication medium between kernel space and user space.
Examining a process using /proc as an example
We now know that cherrytree has the pid of 6832, there is also a directory with that name within /proc.
Change to the pid folder
Below you have a summary of the most important files and directories within each process directory.
Important files with /proc besides the process specific directories