{"id":2334,"date":"2017-04-07T04:11:51","date_gmt":"2017-04-07T04:11:51","guid":{"rendered":"http:\/\/www.fastwebhost.in\/blog\/?p=2334"},"modified":"2019-02-07T04:43:23","modified_gmt":"2019-02-07T04:43:23","slug":"top-process-management-commands-in-linux","status":"publish","type":"post","link":"https:\/\/www.fastwebhost.in\/blog\/top-process-management-commands-in-linux\/","title":{"rendered":"Top Process Management Commands in Linux?"},"content":{"rendered":"<p>When you run any application or program on a Linux server, it creates an instance called <span>process<\/span>.&nbsp;In this article we&#8217;ll discuss following process management commands one by one. These are <span>TOP, HTOP, PS, PSTREE, PGREP, NICE, RENICE , and KIL<\/span>L command.<\/p>\n<p>Each process uses server resources like memory, storage and computing resources to finish the job associated with each process.<\/p>\n<p>Every time a new process is started it is assigned an identification number (ID) which is called process ID or <span>PID<\/span> for short.<\/p>\n<p>As as Linux Administrator or user, you\u2019ll run into situations where you may need to manage all these processes.&nbsp;That\u2019s where the Linux operating system provides a lot of tools to manage running processes.<\/p>\n<h3><span>Here are the few scenarios where you\u2019ll run into.<\/span><\/h3>\n<ul>\n<li>List top running processes and server resource snapshot<\/li>\n<li>To view all the running processes on Linux<\/li>\n<li>To see resources used by each process<\/li>\n<li>Change process priorities and resources available to processes.<\/li>\n<li>Find specific process and change state of that process<\/li>\n<li>Killing running or run away processes<\/li>\n<\/ul>\n<p>You can use Linux process management commands or tools to handle all the above situations.<\/p>\n<h2>top command in Linux or Ubuntu<\/h2>\n<p>The most easiest and essential command for every Linux user is TOP. As the name indicates it displays overall view of the Linux system including TOP running processes.<\/p>\n<p><span><img loading=\"lazy\" decoding=\"async\" alt=\"top command in Linux\" style=\"width: 300px\" src=\"\/\/www.fastwebhost.in\/blog\/wp-content\/uploads\/2017\/04\/top-300x191.png\" width=\"300\" height=\"191\" data-attachment-id=\"2343\"><\/span><\/p>\n<p>You can see server statistics like uptime, load averages and running processes states in the first few lines.<\/p>\n<p>It also shows top running processes with their process identification number(PID), process user, Priority, nice value, %CPU and %memory consumed by the process etc.<\/p>\n<h2>htop&nbsp;command in Linux<\/h2>\n<p>Htop is similar to TOP but more advanced with an interactive process viewer. It shows CPU,Memory and SWAP resources as text graphs.<\/p>\n<p><span><img loading=\"lazy\" decoding=\"async\" alt=\"htop command in Linux\" style=\"width: 300px\" src=\"\/\/www.fastwebhost.in\/blog\/wp-content\/uploads\/2017\/04\/HTOP-300x207.png\" width=\"300\" height=\"207\" data-attachment-id=\"2342\"><\/span><\/p>\n<h3>Comparison between htop and classic top<\/h3>\n<ul>\n<li>In htop you can scroll the list vertically and horizontally to see all processes and full command lines.<\/li>\n<li>In top you are subject to a delay for each unassigned key you press (especially annoying when multi-key escape sequences are triggered by accident).<\/li>\n<li>htop starts faster (top seems to collect data for a while before displaying anything).<\/li>\n<li>In htop you don&#8217;t need to type the process number to kill a process, in top you do.<\/li>\n<li>In htop you don&#8217;t need to type the process number or the priority value to renice a process, in top you do.<\/li>\n<li>In htop you can kill multiple processes at once.<\/li>\n<li>top is older, hence, more tested.<\/li>\n<\/ul>\n<h2>ps command to List Processes<\/h2>\n<p>&#8220;<span>ps<\/span>&#8221; command is the most basic command to see all the running processes on the system. It outputs all running processes of specific user or all users.<\/p>\n<p><span>To see running processes of existing user:<\/span><\/p>\n<p>ps<\/p>\n<p>The output will be similar to this:<\/p>\n<p>PID TTY TIME CMD81775 pts\/0 00:00:00 bash90379 pts\/0 00:00:00 ps<\/p>\n<p><span>To see running processes of all users:<\/span><\/p>\n<p>ps -aux<\/p>\n<p><span>To view specific service\/user running processes:<\/span><\/p>\n<p>ps -ef | grep httpd<\/p>\n<p>\u200b<\/p>\n<h2><span>pstree command in Linux<\/span><\/h2>\n<p>\u201c<span>pstree<\/span>\u201d command will be useful if you want to see running processes hierarchically arranged in tree format.<\/p>\n<p>It\u2019s easy to understand parent and child processes using this command. It is used as a more visual alternative to the ps command. The root of the tree is either init or the process with the given pid.<\/p>\n<h2>nice command to change process priorities<\/h2>\n<p><span>nice<\/span> command is used to assign particular priority, thus giving the process more or less CPU time than other processes.<\/p>\n<p>Process priority values range from <span>-20 to 19<\/span>. Lower the nice value, higher the priority. The default nice value is 0.<\/p>\n<p>It means -20 has highest priority over 15. In order use higher priorities (negative nice values) administrator privileges are required.<\/p>\n<p>When you use the command nice you start a new process and assign it priority (nice) value at the same time.<\/p>\n<p>nice -3 top<\/p>\n<p>The above nice command starts the TOP command or process with nice value of -3.<\/p>\n<h2>renice command in Linux<\/h2>\n<p>&#8220;<span>renice<\/span>&#8221; command is used to change priorities of already running processes. In the above nice example we started process TOP with -3 priority. Let\u2019s change that priority using renice.<\/p>\n<p>renice -5 -p 91524<\/p>\n<p>Command syntax:<\/p>\n<p><span>renice -n -p &#8211; PID<\/span><\/p>\n<h2>pgrep command<\/h2>\n<p>In Linux each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes.<\/p>\n<p>&#8220;<span>pgrep<\/span>&#8221; is the useful command to get list of PID\u2019s for a particular service or program.<\/p>\n<p>For example: If you want to see list of all PID\u2019s of apache web service or httpd.<\/p>\n<p>pgrep httpd<\/p>\n<p>You will get output with PID\u2019s of httpd service.<\/p>\n<h2>kill command<\/h2>\n<p>Every process in Linux responds to signals. Signals are commands or instructions to processes to terminate or modify their behavior.<\/p>\n<p>The default way to send signals to processes is through Kill command.<\/p>\n<p>The very common kill command syntax are:<\/p>\n<p>kill &lt;pid&gt;<\/p>\n<p>This sends the <span>T<\/span><span>ERM<\/span> 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.<\/p>\n<p>If the program is misbehaving and does not exit when given the TERM signal, we can escalate the signal by passing the KILL signal:<\/p>\n<p>kill -9 &lt;pid&gt;<\/p>\n<p>You can send \u201c-KILL\u201d instead of &#8220;-9&#8221;<\/p>\n<p>killall -9 &#8211; httpd &#8211; all instances having the same process name<\/p>\n<p>Here are the SIGNAL codes used by Kill command.<\/p>\n<p> Signal     Value     Action   CommentSIGHUP        1       Term    Hangup detected on controlling terminalor death of controlling processSIGINT        2       Term    Interrupt from keyboardSIGQUIT       3       Core    Quit from keyboardSIGILL        4       Core    Illegal InstructionSIGABRT       6       Core    Abort signal from abortSIGFPE        8       Core    Floating-point exceptionSIGKILL       9       Term    Kill signalSIGSEGV      11       Core    Invalid memory referenceSIGPIPE      13       Term    Broken pipe: write to pipe with noreaders;SIGALRM      14       Term    Timer signal from alarmSIGTERM      15       Term    Termination signalSIGUSR1   30,10,16    Term    User-defined signal 1SIGUSR2   31,12,17    Term    User-defined signal 2SIGCHLD   20,17,18    Ign     Child stopped or terminatedSIGCONT   19,18,25    Cont    Continue if stoppedSIGSTOP   17,19,23    Stop    Stop processSIGTSTP   18,20,24    Stop    Stop typed at terminalSIGTTIN   21,21,26    Stop    Terminal input for background processSIGTTOU   22,22,27    Stop    Terminal output for background process<\/p>\n<p>If you don\u2019t know the process ID, you can use pkill command which works by using process name instead of PID\u2019s.<\/p>\n<p>pkill -9 firefox<\/p>\n<p>The above command will kill firefox program by sending TERM signal.<\/p>\n<h2>Conclusion<\/h2>\n<p>Linux process management is an essential task for each and every Linux administrator. All the above tools allows you to manage system resources efficiently. You can dig deep by using all the options available with each command.<\/p>\n<p>Initially, It could be a little intimidating. Once you get to know it, it\u2019s not that hard.<\/p>\n<p>All the above mentioned process management commands should work fine in all Linux flavors like CentOS, RedHat, Ubuntu and Unix systems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you run any application or program on a Linux server, it creates an instance called process.&nbsp;In this article we&#8217;ll discuss following process management commands one by one. These are TOP, HTOP, PS, PSTREE, PGREP, NICE, RENICE , and KILL command. Each process uses server resources like memory, storage and computing resources to finish the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2350,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[22],"tags":[110],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2334"}],"collection":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/comments?post=2334"}],"version-history":[{"count":3,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":4894,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/posts\/2334\/revisions\/4894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media\/2350"}],"wp:attachment":[{"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/media?parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/categories?post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fastwebhost.in\/blog\/wp-json\/wp\/v2\/tags?post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}