Identify Cpu Memory Intensive Processes Adjust Process Priority With Renice And Kill Processes
General Information
Process wrangling. Get em cow-poke and watch out for zombies.
Identify CPU/memory intensive processes
pgrep
Grep for processes and return the PID.
Search processes for httpd
pgrep httpd -l
- Returns PID (default) and name (-l)
Search for all processes being run by the user called “user”
pgrep -u user -l
All processes not owned by the root user
pgrep -v -u root -l
ps
All processes, user defined display options
ps -eo pid,comm,nice
- -e ⇒ every (all) processes
- -o ⇒ user defined format
GNU Style Options
ps -elf
- -e ⇒ every (all) processes
- -l ⇒ long format
- -f ⇒ full format listing (adds STIME column and displays CMDs full paths)
BSD Style Options
ps aux
- a ⇒ eliminate the “only yourself” restriction
- u ⇒ display user oriented format
- x ⇒ eliminate the “must have tty” restriction
system load
Load average
- percentage of processing power used
- Number of CPUs is important
Get the number of CPUs
grep -c proc /proc/cpuinfo
- -c ⇒ count of matched lines
- proc ⇒ the string searched for in /proc/cpuinfo
View load averages
w OR uptime 17:03:49 up 2:02, 1 user, load average: 0.00, 0.01, 0.04
- shows the: last minute, last 5 mins, last 15 mins load
- last min = 0%, last 5 mins = 1%, last 15 mins = 4%
- If 2 cpus, a load average of 1.00 would be 50% CPU load. (1.00 / 2 = .50)
- If 2 cpus, a load average of 2.00 would be 100% CPU load. (2.00 / 2 = 1.00)
Percentage of total processing power in use = load average / (# of cpus)
top
Open top
top
- Sorted by %cpu used by default
Interactive commands
- z ⇒ toggle colors (easier to see highlighted columns)
- x ⇒ highlight current sort column
- 1 ⇒ expand CPU list to see individual usage
- M ⇒ sort by %memory used
- P ⇒ sort by %cpu used
- < or > ⇒ change sort column
- r ⇒ (renice) enter pid to renice, then nice level (selects top PID by default, can change)
- k ⇒ (kill) enter pid, then signal to send (selects top PID by default, can change)
Start top with a screen update delay of 2 seconds
top -d 2
- Default is update the display every 3 seconds
adjust process priority with renice
Highest priority ⇒ -20 (the least nice to other processes)
Lowest priority ⇒ 19 (the most nice to other processes)
Default nice level if nice run, but level not specified ⇒ 10
Start program with a nice level of 5
nice -n 5 httpd
Change nice level of a running process to -5
renice -n -5 2879
Change nice level of all running httpd processes to -10
renice -n -10 $(pgrep httpd)
kill processes
List kill signals
kill -l
- 1 (SIGHUP) ⇒ hang up, similar to closing a terminal window
- 2 (SIGINT) ⇒ keyboard interrupt (ctrl+c)
- 3 (SIGQUIT) ⇒ ask process to quit
- 9 (SIGKILL) ⇒ Kill process immediately, not blockable
- 15 (SIGTERM) ⇒ Asks process to terminate cleanly. Default when nothing passed.
- 18 (SIGCONT) ⇒ continue process that was stopped
- 19 (SIGSTOP) ⇒ stop the process, not ignorable
- 20 (SIGTSTP) ⇒ stop that can be ignored
Kill process by name (instead of PID)
pkill httpd
- Kills all process ids named httpd, sending signal 15 (SIGTERM)
Remove a user's ssh session (kick user off system)
pkill -u rjones sshd
Kill all processes started from a specific terminal
pkill -t pts/1
- This does NOT kick them off the system, only kills their running programs started from that session.
Stop a process that is job #1
kill -SIGSTOP %1
- Alt command: kill -19 %1
Contine the process that is job #1
kill -SIGCONT %1
- Alt command: kill -18 %1
Kill an individual process with PID 2594
kill -SIGKILL 2594
- Alt command: kill -9 2594