====== Unix Processes ====== A **process** is a program that is currently running on a machine. This document describes how to list running processes and how to interact with those processes. You may have many programs running at once, and each running program is known as a process. You may even have the same program running multiple times simultaneously, and each one is a separate **instance** of the same program. Each one is also a separate process. All of the commands listed below require the use of a terminal window. The terminal under which a process is running is also known as the process' **controlling terminal**. Already beginning to sound complicated? Don't worry, that's why all this is written down. ====== Required Reading ====== - [[nimbios_11.04_intro|NIMBioS' Ubuntu-11.04 Operating Environment Introduction]] - [[unix_101|Unix 101]] ====== Listing Processes ====== The ''ps'' command is used to list processes that you have are running on the current machine under your current login. If you open a terminal window and type ''ps'', you will see something like this: $ ps PID TTY TIME CMD 2088 pts/13 00:00:00 bash 2096 pts/13 00:00:00 ps The process ''bash'' is the shell that your terminal window is running, and the ''ps'' is the command that you just entered at the shell prompt. But these are just the processes being run by the current terminal window. You actually have more processes than this running. To see all the processes being run by you, try the following command: $ ps -u For example: $ ps -u peek PID TTY TIME CMD 1046 ? 00:00:06 chrome 1055 pts/11 00:00:00 bash 1063 pts/11 00:00:00 ssh [...stuff deleted for brevity...] The above command will give you a listing of all the processes that I (Michael Peek) have running on my computer. The first thing you will notice is that there is a lot of them. This is normal. Many of these processes are started automatically when you log in as part of your desktop environment. The second thing you will notice is that most of these names are likely to be unfamiliar -- that's okay. Since many of the are associated with your desktop environment it's not important that you be familiar with them. (They're like plumbing. If you notice the plumbing, then there's something wrong with the plumbing.) And finally, the last thing you might notice is that some of the processes seem to have been truncated -- and this would be true. In fact, if you want to see a more comprehensive list of the processes running, you would add the ''-f'' command line option, which tells the ''ps'' command to print it's output in full-format: $ ps -f -u peek UID PID PPID C STIME TTY TIME CMD peek 1046 2996 0 13:48 ? 00:00:06 /opt/google/chrome/chrome --type=renderer --lang=en-US --force-fieldtest=Cach peek 1055 16071 0 13:48 pts/11 00:00:00 bash peek 1063 1055 0 13:49 pts/11 00:00:00 ssh -l root db1 [...stuff deleted for brevity...] This is a lot to look at, but where possible ''ps'' will give you the entire command line used to start each process. This can be useful. But wait, there's even more! These are just the listings for the processes that **you** are running -- this doesn't count system processes or processes being run by other users! To get a comprehensive list of **all** processes running on the machine, use the following command: $ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Aug20 ? 00:00:01 /sbin/init root 2 0 0 Aug20 ? 00:00:00 [kthreadd] root 3 2 0 Aug20 ? 00:00:00 [migration/0] [...stuff deleted for brevity...] Not only will this give you a list of every process running on the machine, but it will also tell you which user is running the process -- very handy. Exactly how we will get to farther down on this page. :!: NOTE: Notice the column labeled PID in the output from each ''ps'' command above. PID stands for Process IDentifier, and it's a number that uniquely identifies a particular instance of a program. For instance, if you run a program, exit that program, and then run it again you will find that the second time that you run the program that program has a different PID number than the first time you ran it. Likewise, if you run five instances of the same program at once, each instance will have a separate PID number. This PID number is used to both identify and communicate with the process. ====== Interacting With Processes ====== ===== Suspending A Process ===== When running a program in a terminal, you can interrupt the program by holding down the control key and pressing 'z' (herefore to be referred to as simply 'cntrl-z'). When you do this, the program is suspended and you are returned to the shell prompt. This is what you see on the terminal: $ vi [1]+ Stopped vi ===== Resuming A Process ===== To resume a suspended process, type the command ''fg''. ===== Stopping A Process ===== To stop a process all together, you would press cntrl-c. Most programs have their own method of shutting down, but if a process is hung then cntrl-c will usually force it to exit. :!: NOTE: Cntrl-c does not always work on processes involved in I/O with a device, as the bottleneck can be lower down at the kernel level. But if you press cntrl-c and the process does not exit, then it will exit just as soon as the kernel relinquishes control. ===== Signals ===== Suspending and resuming a process can also be accomplished by sending a signal to the process. Signals are sent via the ''kill'' command. The useful thing about signals is that they apply to any process running under your username, and not just the processes running under a single terminal. For instance, say that you have a process called ''xbiff'' running: $ ps -ef | grep xbiff peek 3315 2088 0 16:00 pts/13 00:00:00 xbiff peek 3327 2145 0 16:00 pts/14 00:00:00 grep xbiff If you want to suspend this process, you would type: kill -SIGTSTP 3315 This is the equivalent of typing cntrl-z on the terminal under which ''xbiff'' is running. Likewise, once ''xbiff'' is suspended, you can make it start up again by typing: kill -SIGCONT 3315 Which is the equivalent of typing ''fg'' on the terminal under which ''xbiff'' is running. There are many other signals that you can send to a process too, but the most used ones are: ^ Terminal command / keystroke ^ ''kill'' signal ^ Description ^ | cntrl-z | SIGTSTP | Suspend the process | | fg | SIGCONT | Resume the process | | cntrl-c | SIGINT | Ask the process to terminate (Cleanly closes all files) | | | SIGKILL | Force the process to terminate (Does not cleanly close files!) |