Process management
From NewbieDOC
- Romain Lerallut
- rom1 AT users DOT sourceforge DOT net (author)
- Chris Lale
- chrislale AT users DOT berlios DOT de (publisher)
Go to NewbieDOC index
Revision History
Revision v0.01 | 5 May 2001 | Revised by Romain Lerallut | ||||||||||
Started this whole thing. http://newbiedoc.sourceforge.net/utils/process.html.en | ||||||||||||
| ||||||||||||
Revision v0.02 | 24 May 2001 | Revised by Romain Lerallut | ||||||||||
Added the FDL License. | ||||||||||||
| ||||||||||||
Revision v0.03 | 30 May 2001 | Revised by Romain Lerallut | ||||||||||
Changes in the title page. | ||||||||||||
| ||||||||||||
Revision v0.04 | 26th January 2006 | Revised by Chris Lale | ||||||||||
Adapted for NewbieDOC wiki by converting source to wikitext. Made some cosmetic changes. | ||||||||||||
| ||||||||||||
Revision v0.05 | 12th March 2007 | Revised by Chris Lale | ||||||||||
Modified "Killing processes" section after comments about the scripts. Added a link to the discussion page for comments. | ||||||||||||
| ||||||||||||
Revision v0.06 | 26th November 2007 | Revised by Chris Lale | ||||||||||
Reformatted for newbiedoc package. | ||||||||||||
A document about how to manage processes under Linux.
1 IntroductionOne of the strengths of Linux is its excellent process management. A crashing process cannot bring down the system with it, and a lot of work has been done on multitasking so that each process gets its share. 1.1 CreditsThanks to the NewbieDoc team, to Ken Thomson and Dennis Ritchie for Unix, and to the Linux developers for Linux ! 1.2 FeedbackAs usual, comments, corrections, flames, are all equally welcome ! 2 Unix and processesMost of what will be said in this doc is not Linux-specific, but should apply to most Unix systems A process running on a Unix system can be managed using five numbers:
Note: Each process has a parent (except init, being The First) and keeps the parent's PID stored somewhere called PPID.
The lower a job's priority number, the more important it is, go figure... Use nice (renice) to define (modify) a process's priority when starting it (any time). 3 Process Management3.1 Finding info about a processThere are two tools used to find info about running processes: 3.1.1 Static Process Management: ps
At the shell prompt, type $ ps you should see something like: PID TTY TIME CMD 10923 pts/3 00:00:00 bash 12494 pts/3 00:00:00 ps which are the currently running process that are attached to your shell. To see all your processes, type $ ps xu with x for "all processes even those that are not launched from a terminal" and u for slightly more detailed info To see all processes, including those belonging to others type $ ps axu See the 3.1.2 Dynamic Process Management: top
just type $ top and get my meaning. You'll see all your running processes ordered by decreasing CPU usage. Common interactive options are:
Then it reports global CPU and memory usage Then come the list of running processes. Interesting fields are:
The remaining columns should be easy to understand :-)... 3.2 Signals3.2.1 What are signals?In Unix the owner of a process (or the superuser) can send a signal to a process he owns (any process for the SU). Some of these signals can be recognized by processes which can act accordingly. Example: say you have a laptop, and a battey-monitoring tool. If the battery runs low, your monitoring program can send signals (SIGPWR, I guess) to your running programs so that they shut down correctly and then poweroff the computer. 3.2.2 Which signals should I know of?
3.2.3 How do I send a signal?It is simple. You must know the PID of a process and then: $ kill -signal_number PID for example: $ kill -9 123 or $ kill -SIGKILL 123 will terminate with maximum prejudice process #123. You can find the conversion table between signals' names and numbers by doing $ kill -l 3.3 Nice and priorities Tip: If you want to run a process without hogging 99% of the CPU and upset other users, nice is for you. A process is usually run with the priority of its parent. To know your shell's priority type: $ nice To run a niced command type: $ nice my_command It will (by default) add 10 to your shell's priority and define it as the process' priority. If your shell's priority is 0, your new process will have priority of 10. To specify the nice-level (relative to current shell's priority): $ nice -n 14 so the priority of the new process will be current_shell_priority+14. 3.4 Renice a job's prioritiesTo renice a job's priority, use renice. $ renice +5 my_pid You can also specify "all the processes of a user": # renice +20 -u patient_guy This will add 20 to the priority of all patient_guy's processes. (you usually need to be root to do this, and to be able to run fast :-) 4 Tips and Tricks4.1 Killing processesLet's face it: the only time we use kill to send a signal is to kill a process. And that's why it's called kill. I usually have netscape crashes. Here is a small script that might be useful: #!/bin/sh ps xu | grep netscape | grep -v grep | awk '{ print $2 }' | xargs kill -9
Unfortunately is uses five processes. Also another process containing the word 'netscape'would also be killed by this script. It could be done better like this. $ ps -C netscape -o pid= | xargs kill -9 There's a slight problem with the '-C' option when using 'ps' - it doesn't work with the 'U/-U/-u/' as one would expect it to work. However if you are not root, this is not a problem. Alternatively, you could use pgrep (which is also in the procps package). $ pgrep -u $USER -x netscape | xargs kill -9 Better yet: $ pkill -KILL -u $USER -x netscape One more point: only use the KILL signal as a last resort. For alternatives see Useless use of kill -9.
5 Appendix A: LicenceCopyright (c) 2001 Romain Lerallut, rom1@users.sourceforge.net. Copyright (c) 2006-2007 Chris Lale, chrislale AT users DOT berlios DOT de
Go to NewbieDOC index |