- All You Need To Know About Processes in Linux [Comprehensive Guide]
- Types of Processes
- What is Daemons
- Creation of a Processes in Linux
- How Does Linux Identify Processes?
- The Init Process
- Starting a Process in Linux
- Linux Background Jobs
- States of a Process in Linux
- How to View Active Processes in Linux
- 1. ps Command
- 2. top – System Monitoring Tool
- 3. glances – System Monitoring Tool
- How to Control Processes in Linux
- Sending Signals To Processes
- Changing Linux Process Priority
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Managing Processes In Ubuntu Linux
- Processes
- Background Processes
- Foreground processes
- Listing and managing processes with ps command
- Listing and Managing Process With top Command
- Listing & Managing Processes With System Monitor
- Killing a process with kill and killall
- Conclusion
- About the author
- Usama Azad
All You Need To Know About Processes in Linux [Comprehensive Guide]
In this article, we will walk through a basic understanding of processes and briefly look at how to manage processes in Linux using certain commands.
A process refers to a program in execution; it’s a running instance of a program. It is made up of the program instruction, data read from files, other programs or input from a system user.
Types of Processes
There are fundamentally two types of processes in Linux:
- Foreground processes (also referred to as interactive processes) – these are initialized and controlled through a terminal session. In other words, there has to be a user connected to the system to start such processes; they haven’t started automatically as part of the system functions/services.
- Background processes (also referred to as non-interactive/automatic processes) – are processes not connected to a terminal; they don’t expect any user input.
What is Daemons
These are special types of background processes that start at system startup and keep running forever as a service; they don’t die. They are started as system tasks (run as services), spontaneously. However, they can be controlled by a user via the init process.
Linux Process State
Creation of a Processes in Linux
A new process is normally created when an existing process makes an exact copy of itself in memory. The child process will have the same environment as its parent, but only the process ID number is different.
There are two conventional ways used for creating a new process in Linux:
- Using The System() Function – this method is relatively simple, however, it’s inefficient and has significantly certain security risks.
- Using fork() and exec() Function – this technique is a little advanced but offers greater flexibility, speed, together with security.
How Does Linux Identify Processes?
Because Linux is a multi-user system, meaning different users can be running various programs on the system, each running instance of a program must be identified uniquely by the kernel.
And a program is identified by its process ID (PID) as well as it’s parent processes ID (PPID), therefore processes can further be categorized into:
- Parent processes – these are processes that create other processes during run-time.
- Child processes – these processes are created by other processes during run-time.
The Init Process
Init process is the mother (parent) of all processes on the system, it’s the first program that is executed when the Linux system boots up; it manages all other processes on the system. It is started by the kernel itself, so in principle it does not have a parent process.
The init process always has process ID of 1. It functions as an adoptive parent for all orphaned processes.
You can use the pidof command to find the ID of a process:
Find Linux Process ID
To find the process ID and parent process ID of the current shell, run:
Find Linux Parent Process ID
Starting a Process in Linux
Once you run a command or program (for example cloudcmd – CloudCommander), it will start a process in the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal and a user can send input it:
Start Linux Interactive Process
Linux Background Jobs
To start a process in the background (non-interactive), use the & symbol, here, the process doesn’t read input from a user until it’s moved to the foreground.
Start Linux Process in Background
You can also send a process to the background by suspending it using [Ctrl + Z] , this will send the SIGSTOP signal to the process, thus stopping its operations; it becomes idle:
To continue running the above-suspended command in the background, use the bg command:
To send a background process to the foreground, use the fg command together with the job ID like so:
Linux Background Process Jobs
States of a Process in Linux
During execution, a process changes from one state to another depending on its environment/circumstances. In Linux, a process has the following possible states:
- Running – here it’s either running (it is the current process in the system) or it’s ready to run (it’s waiting to be assigned to one of the CPUs).
- Waiting – in this state, a process is waiting for an event to occur or for a system resource. Additionally, the kernel also differentiates between two types of waiting processes; interruptible waiting processes – can be interrupted by signals and uninterruptible waiting processes – are waiting directly on hardware conditions and cannot be interrupted by any event/signal.
- Stopped – in this state, a process has been stopped, usually by receiving a signal. For instance, a process that is being debugged.
- Zombie – here, a process is dead, it has been halted but it’s still has an entry in the process table.
How to View Active Processes in Linux
There are several Linux tools for viewing/listing running processes on the system, the two traditional and well known are ps and top commands:
1. ps Command
It displays information about a selection of the active processes on the system as shown below:
List Linux Active Processes
2. top – System Monitoring Tool
List Linux Running Processes
Read this for more top usage examples: 12 TOP Command Examples in Linux
3. glances – System Monitoring Tool
glances is a relatively new system monitoring tool with advanced features:
Glances – Linux Process Monitoring
There are several other useful Linux system monitoring tools you can use to list active processes, open the link below to read more about them:
How to Control Processes in Linux
Linux also has some commands for controlling processes such as kill, pkill, pgrep and killall, below are a few basic examples of how to use them:
Control Linux Processes
To learn how to use these commands in-depth, to kill/terminate active processes in Linux, open the links below:
Note that you can use them to kill unresponsive applications in Linux when your system freezes.
Sending Signals To Processes
The fundamental way of controlling processes in Linux is by sending signals to them. There are multiple signals that you can send to a process, to view all the signals run:
List All Linux Signals
To send a signal to a process, use the kill, pkill or pgrep commands we mentioned earlier on. But programs can only respond to signals if they are programmed to recognize those signals.
And most signals are for internal use by the system, or for programmers when they write code. The following are signals which are useful to a system user:
- SIGHUP 1 – sent to a process when its controlling terminal is closed.
- SIGINT 2 – sent to a process by its controlling terminal when a user interrupts the process by pressing [Ctrl+C] .
- SIGQUIT 3 – sent to a process if the user sends a quit signal [Ctrl+D] .
- SIGKILL 9 – this signal immediately terminates (kills) a process and the process will not perform any clean-up operations.
- SIGTERM 15 – this a program termination signal (kill will send this by default).
- SIGTSTP 20 – sent to a process by its controlling terminal to request it to stop (terminal stop); initiated by the user pressing [Ctrl+Z] .
The following are kill commands examples to kill the Firefox application using its PID once it freezes:
To kill an application using its name, use pkill or killall like so:
Changing Linux Process Priority
On the Linux system, all active processes have a priority and certain nice value. Processes with higher priority will normally get more CPU time than lower priority processes.
However, a system user with root privileges can influence this with the nice and renice commands.
From the output of the top command, the NI shows the process nice value:
List Linux Running Processes
Use the nice command to set a nice value for a process. Keep in mind that normal users can attribute a nice value from zero to 20 to processes they own.
Only the root user can use negative nice values.
To renice the priority of a process, use the renice command as follows:
Check out our some useful articles on how to manage and control Linux processes.
That’s all for now! Do you have any questions or additional ideas, share them with us via the feedback form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Managing Processes In Ubuntu Linux
Processes
Running an instance of a program is called a process. In Linux, process id (PID) is used to represent a process that is distinctive for every process. There are two types of processes,
- Background processes
- Foreground processes
Background Processes
Background processes start in the terminal and run by themselves. If you run a process in the terminal, its output will be displayed in a terminal window, and you can interact with it, but if you don’t need to interact with the process, you can run it in the background. If you want to run a process in the background, just add a “&” sign at the end of the command, and it will start running in the background; it will save you time, and you will be able to start another process. For listing processes running in the background, use the command ‘jobs.’ It will display all the running processes in the background.
For example, upgrading is a long process in Linux. It takes too much time, and if you want to do other stuff while the system is upgrading, use the background command.
$ sudo apt-get upgrade -y &
It will start running in the background. And you can interact with other programs in the meanwhile. You can check how many and which processes are running in the background by typing this command.
$ jobs
[ 1 ] + Running sudo apt-get upgrade -y &
Foreground processes
All the processes that we run in the terminal are, by default, run as foreground processes. We can manage them by foreground and background commands.
You can bring any background process listed in jobs to the foreground by typing the ‘fg’ command followed by the background process number.
$ fg % 1
sudo apt-get upgrade -y
And if you want to take this process to the background type this command.
Listing and managing processes with ps command
The listing process with the ps command is one of the oldest ways to view the terminal running processes. Type ps command to list which processes are running and how much system resource they are using and who is running them.
$ ps u
USER PID % CPU % MEM VSZ RSS TTY STAT START TIME COMMAND
Jim 1562 0.0 0.0 164356 6476 tty2 Ssl+ 13 :07 0 :00 shell
Jim 1564 5.2 0.9 881840 78704 tty2 Sl+ 3 :07 13 : 13 dauth
Jim 2919 0.0 0.0 11328 4660 pts / 0 Ss 13 :08 0 :00 bash
Jim 15604 0.0 0.0 11836 3412 pts / 0 R+ 17 : 19 0 :00 ps u
. snip.
The user column shows the user name in the above table, and PID shows the process id. You can use the PID to kill or send the kill signal to a process. %CPU shows CPU percentage processor, and %MEM shows random access memory usage. To kill a process, type.
$ kill [ process id ( PID ) ]
$ kill -9 [ process id ( PID ) ]
Use the ps aux command to see all running processes and add a pipe to see it in order.
If you want to rearrange the columns, you can do it by adding a flag -e for listing all the processes and -o for indicating for the columns by keywords in the ps command.
$ ps -eo pid,user,uid, % cpu, % mem,vsz,rss, comm
PID USER UID % CPU % MEM VSZ RSS COMMAND
1 root 0 0.1 0.1 167848 11684 systemed
3032 jim 1000 16.5 4.7 21744776 386524 chrome
. snip.
Options for ps command.
u option is used for listing the processes by the users.
f option is used to display the full listing.
x option is used to display information about the process without a terminal.
e option is used to display extended information.
a option is used for listing all the processes with the terminal.
v option is used to display virtual memory format.
Flags for ps command.
-e flag is used to see every process on the system.
-u flag is used to see processes running as root.
-f flag is used for a full listing of processes.
-o flag is used for listing the processes in the desired column.
pstree is another command to list the processes; it shows the output in a tree format.
Options for pstree command
-n is used for sorting processes by PID.
-H is used for highlighting processes.
-a is used for showing output, including command-line arguments.
-g is used for showing processes by group id.
-s is used for sowing the tree or specific process.
[userName] is used for showing processes owned by a user.
$ pstree jim
pgrep
With the pgrep command, you can find a running process based on certain criteria. You can use the full name or abbreviation of the process to find or by username or other attributes. pgrep command follows the following pattern.
$ pgrep -u jim chrome
Options for pgrep command
-i is used for searching case insensitive
$ Pgrep -i firefox
-d is used for delimiting the output
-u is used for finding process owned by a user
-a is used for listing processes alongside their commands
-c is used for showing the count of matching processes
-l is used for listing processes and their name
$ Pgrep -u jim -l
pkill
With the pkill command, you can send a signal to a running process based on certain criteria. You can use the full name or abbreviation of the process to find or by username or other attributes. pgrep command follows the following pattern.
$ Pkill -9 chrome
Options for pkill command
–signal is used for sending a signal e.g. SIGKILL, SIGTERM, etc.
$ Pkill —signal SIGTERM vscode
-HUP is used for reloading a process
$ Pkill -HUP syslogd
-f is used for killing processes based on the full command-line.
$ Pkill -f “ ping 7.7.7.7”
-u is used for killing all the processes owned by a user.
-i is used for case insensitive killing of the process by pkill.
$ Pkill -i firefox
-9 is used for sending a kill signal.
-15 is used for sending a termination signal.
$ Pkill -15 vlc
lsof ( List of Open Files )
This command-line utility is used for listing files opened by several processes. And as we know, all UNIX/Linux systems recognize everything as a file, so it is convenient to use the lsof command to list all opened files.
In the above table of lsof command, FD represents file description, cwd represents the current working directory, txt means text file, mem means memory-mapped files, mmap means memory-mapped devices, REG represents a regular file, DIR represents Directory, rtd means root directory. There are other options you can use with the lsof command.
Options for lsof command.
-c is used for the listing of open files by their process name.
-u is used for the listing of open files by a user.
-i is used for the listing of processes executing on a port.
+D is used for the listing of open files under a directory.
-p is used for the listing of open files by a process.
Listing and Managing Process With top Command
With the top command, you can display a real-time view of system processes running. It displays the processes depending upon CPU usage. You can sort the column according to you. The top command also provides some information about your system, like how long the system has been running up or how many users are attached to the system and how many processes are running, how much CPU and RAM is being used, and a listing of each process.
Type the command top to list down the processes running.
Tasks: 291 total, 1 running, 290 sleeping, 0 stopped, 0 zombie
% Cpu ( s ) : 2.3us, 0.3sy, 0.0ni, 97.0id, 0.3wa, 0.0hi, 0.0si, 0.0st
MiB Mem: 7880.6 total, 1259.9 free , 3176 used, 3444.4 buff / cache
MiB Swap: 2048.0 total, 2048.0 free , 0.0 used. 4091.8 avail Mem
PID USER PR NI VIRT RES SHR S % CPU % MEM TIME+ COMMAND
3241 jim 20 0 20.7g 33512 10082 S 1.7 4.2 0 : 54.24 chrome
3327 jim 20 0 4698084 249156 86456 S 1.3 3.1 1 : 42.64 chrome
2920 jim 20 0 955400 410868 14372 S 1.0 5.1 7 : 51.04 chrome
3423 jim 20 0 4721584 198500 10106 S 1.0 2.5 0 : 49.00 chrome
3030 jim 20 0 458740 114044 66248 S 0.7 1.4 3 : 00.47 chrome
3937 jim 20 0 4610540 104908 72292 S 0.7 1.3 0 : 05.91 chrome
1603 jim 20 0 825608 67532 40416 S 0.3 0.8 3 : 13.52 Xorg
1756 jim 20 0 4154828 257056 10060 S 0.3 3.2 5 : 53.31 gnome-s+
1898 jim 20 0 289096 29284 5668 S 0.3 0.4 1 : 06.28 fusuma
3027 jim 20 0 587580 14304 75960 S 0.3 1.8 9 : 43.59 chrome
3388 jim 20 0 4674192 156208 85032 S 0.3 1.9 0 : 13.91 chrome
3409 jim 20 0 4642180 140020 87304 S 0.3 1.7 0 : 15.36 chrome
3441 jim 20 0 16.5g 156396 89700 S 0.3 1.9 0 : 25.70 chrome
You can also do some actions with the top command to make changes in running processes; here is the list below.
- u by pressing “u” you can display a process running by a certain user.
- M by pressing “M” you can arrange by RAM usage rather than CPU usage.
- P by pressing “P” you can sort by CPU usage.
- 1 by pressing “1” switch between CPUs usage if there are more than one.
- R by pressing “R” you can make your output sort reverse.
- h by pressing “h” you can go to help and press any key to return.
Note which process is consuming more memory or CPU. Those processes which are consuming more memory can be killed, and those processes which are consuming more CPU can be reniced to give them less importance to the processor.
Kill a process at the top: Press k and write the Process ID you want to kill. Then type 15 or 9 to kill normally or immediately; you can also kill a process with a kill or killall command.
Renice a process at the top: Press r and write the PID of the process you want to be reniced. It will ask you to type the PID of the process and then the value of nicing you want to give this process between -19 to 20 (-19 means the highest importance and 20 means lowest importance).
Listing & Managing Processes With System Monitor
Linux has a system monitor gnome to show the running processes more dynamically. To start the system monitor, press the windows key and type the system monitor, click on its icon, and you can see processes in columns. By right-clicking them, you can kill, stop, or renice the process.
The running processes are displayed with user accounts in alphabetical order. You can sort the processes by any field headings like CPU, Memory, etc., just click on them, and they will be sorted; for example, click on CPU to see which process is consuming the most CPU power. To manage processes, right-click on them and select the option you want to do with the process. To manage the process select the following options.
- Properties- show other settings related to a process.
- Memory Maps- show system memory maps to show which library and other components are being used in memory for the process.
- Open file- shows which files are opened by the process.
- Change Priority- display a sidebar from which you can renice the process with the options from very high to very low and custom.
- Stop- pauses the process until you select to continue.
- Continue- restarts a paused process.
- Kill- Force kills a process instantly.
Killing a process with kill and killall
kill, and killall command is used for Killing/ending a running process. These commands can also be used for sending a valid signal to a running process, like telling a process to continue, end, or reread configuration files, etc. Signals can be written in both ways by numbers or by name. The following are some commonly used signals.
Signal Number Description
SIGHUP 1 Detects hang-up signal on controlling terminal.
SIGINT 2 Interpreted from keyboard.
SIGQUIT 3 Quit from the keyboard.
SIGILL 4 Illegal instructions.
SIGTRAP 5 Is used for tracing a trape.
SIGABRT 6 is used for aborting signal from abort(3).
SIGKILL 9 Is used for sending a kill signal.
SIGTERM 15 Is used for sending a termination signal.
SIGCONT 19,18,25 Is used to continue a process if stopped.
SIGSTOP 17,19,23 Is used for stopping processes.
Different values of SIGCONT and SIGSTOP are used in different Unix/Linux operating systems. For detailed information about signals type man 7 signal terminal.
Using kill Command For Sending Signal To Process By PID.
Note the process to which you want to send a kill signal. You can find the process id (PID) by ps or top command.
PID USER PR NI VIRT RES SHR S % CPU % MEM TIME+ COMMAND
7780 jim 20 0 12596 4364 3460 R 33.3 3.2 13 : 54 : 12 top
The top process is consuming 33.3% of the CPU. If you want to kill this process to save CPU usage, here are some ways to end this process with the kill command.
$ kill -15 7780 or $ kill -SIGTERM 7780
$ kill -9 7780 or $ kill -SIGKILL 7780
Using killall Command To Send Signals To A Process By Name.
With the killall command, you don’t have to search for process id; you can send a kill signal to a process by name rather than process id. It can also kill more processes than you want if you are not careful, e.g., “killall chrome” will kill all chrome processes, including those you don’t want to kill. Sometimes it is useful to kill processes of the same name.
Like the kill command, you can type the signals by name or by number in the killall command. Kill any running process with the killall command; you only have to type its name and the signal you want to send. e.g., send a kill signal process firefox using the killall command, write the below command.
$ killall -9 firefox
$ killall SIGKILL chrome
Changing the process priority with nice and renice
Every process on your Linux system has an excellent value, and it is between -19 to 20. It decided which process would get more CPU access in the system. The lower the value of the nice, the more access a process has to the CPU process. Like -16 nice values have more access to the CPU than 18 nice values. Only a user with root privileges can assign a negative value of nice. A normal user can only assign the value of “nice” between 0 to 19. A regular user can only assign higher nice values and on its own processes. A root user can set any nice value to any process.
If you want to give a process more accessible to the CPU usage by assigning the nice value, type the following command.
And renice the process
$ renice -n -6 3612
Conclusion
Here is the guide to managing your Linux system with ps, top, lsof, pstree, pkilll, kill, killall, nice, renice, etc. Some processes consume most of the CPU usage and RAM; knowing how to manage them increases your system speed and performance and gives you a better environment to run any processes you want more efficiently.
About the author
Usama Azad
A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14
Источник