Home » Categories » Multiple Categories |
How To Use ps, kill, and nice to Manage Processes in Linux |
Article Number: 177 | Rating: Unrated | Last Updated: Thu, Dec 26, 2013 at 5:10 AM
|
A Linux server, like any other computer you may be familiar with, runs applications. To the computer, these are considered "processes". While Linux will handle the low-level, behind-the-scenes management in a process's life-cycle, you will need a way of interacting with the operating system to manage it from a higher-level. In this guide, we will discuss some simple aspects of process management. Linux provides an abundant collection of tools for this purpose. We will explore these ideas on an Ubuntu 12.04, but any modern Linux distribution will operate in a similar way. How To View Running Processes in LinuxtopThe easiest way to find out what processes are running on your server is to run the
The top chunk of information give system statistics, such as system load and the total number of tasks. You can easily see that there is 1 running process, and 55 processes are sleeping (aka idle/not using CPU resources). The bottom portion has the running processes and their usage statistics. htopAn improved version of
If we run the
How To Use ps to List Processes Both However, these tools are not always flexible enough to adequately cover all scenarios. A powerful command called When called without arguments, the output can be a bit lack-luster:
This output shows all of the processes associated with the current user and terminal session. This makes sense because we are only running To get a more complete picture of the processes on this system, we can run the following:
These options tell To see a tree view, where hierarchal relationships are illustrated, we can run the command with these options:
As you can see, the process A Note About Process IDsIn Linux and Unix-like systems, each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes. A quick way of getting the PID of a process is with the
This will simply query the process ID and return it. The first process spawned at boot, called init, is given the PID of "1".
This process is then responsible for spawning every other process on the system. The later processes are given larger PID numbers. A process's parent is the process that was responsible for spawning it. If a process's parent is killed, then the child processes also die. The parent process's PID is referred to as the PPID. You can see PID and PPID in the column headers in many process management applications, including Any communication between the user and the operating system about processes involves translating between process names and PIDs at some point during the operation. This is why utilities tell you the PID. How To Send Processes Signals in LinuxAll processes in Linux respond to signals. Signals are an os-level way of telling programs to terminate or modify their behavior. How To Send Processes Signals by PIDThe most common way of passing signals to a program is with the As you might expect, the default functionality of this utility is to attempt to kill a process: kill PID_of_target_process This sends the TERM signal to the process. The TERM signal tells the process to please terminate. This allows the program to perform clean-up operations and exit smoothly. If the program is misbehaving and does not exit when given the TERM signal, we can escalate the signal by passing the kill -KILL PID_of_target_process This is a special signal that is not sent to the program. Instead, it is given to the operating system kernel, which shuts down the process. This is used to bypass programs that ignore the signals sent to them. Each signal has an associated number that can be passed instead of the name. For instance, You can pass "-15" instead of "-TERM", and "-9" instead of "-KILL". How To Use Signals For Other PurposesSignals are not only used to shut down programs. They can also be used to perform other actions. For instance, many daemons will restart when they are given the sudo kill -HUP pid_of_apache The above command will cause Apache to reload its configuration file and resume serving content. You can list all of the signals that are possible to send with kill by typing:
How To Send Processes Signals by NameAlthough the conventional way of sending signals is through the use of PIDs, there are also methods of doing this with regular process names. The
The above command is the equivalent of:
If you would like to send a signal to every instance of a certain process, you can use the
The above command will send the TERM signal to every instance of firefox running on the computer. How To Adjust Process PrioritiesOften, you will want to adjust which processes are given priority in a server environment. Some processes might be considered mission critical for your situation, while others may be executed whenever there might be leftover resources. Linux controls priority through a value called niceness. High priority tasks are considered less nice, because they don't share resources as well. Low priority processes, on the other hand, are nice because they insist on only taking minimal resources. When we ran
Nice values can range between "-19/-20" (highest priority) and "19/20" (lowest priority) depending on the system. To run a program with a certain nice value, we can use the nice -n 15 command_to_execute This only works when beginning a new program. To alter the nice value of a program that is already executing, we use a tool called renice 0 PID_to_prioritize Note: While nice operates with a command name by necessity, renice operates by calling the process PID ConclusionProcess management is a topic that is sometimes difficult for new users to grasp because the tools used are different from their graphical counterparts. However, the ideas are familiar and intuitive, and with a little practice, will become natural. Because processes are involved in everything you do with a computer system, learning how to effectively control them is an essential skill. |
Attachments
There are no attachments for this article.
|
How to Add a Swap File on an Arch Linux Cloud Server
Viewed 2632 times since Fri, Dec 27, 2013
Installing and Using the Vim Text Editor on a Cloud Server
Viewed 2520 times since Fri, Dec 27, 2013
How To Create a SSL Certificate on Apache for Ubuntu 12.04
Viewed 3305 times since Mon, Dec 23, 2013
How To Copy Files With Rsync Over SSH
Viewed 6520 times since Fri, Dec 27, 2013
How To Set Up mod_security with Apache on Debian/Ubuntu
Viewed 4063 times since Thu, Dec 26, 2013
How To Install and Configure Django with Postgres, Nginx, and Gunicorn
Viewed 4252 times since Fri, Jan 3, 2014
How To Set Up vsftpd on CentOS 6
Viewed 2212 times since Thu, Dec 26, 2013
How To Use a Simple Bash Script To Restart Server Programs
Viewed 3236 times since Fri, Dec 27, 2013
How To Create Nagios Plugins With Python On CentOS 6
Viewed 3627 times since Sat, Jan 4, 2014
How To Create Nagios Plugins With Bash On Ubuntu 12.10
Viewed 2135 times since Sat, Jan 4, 2014
|