When a command or program is run, it is assigned its own memory, I/O access, and other resources, referred to as its environment. The running program is called a process and it is assigned a unique tracking ID known as its process ID.
The maximum process ID can be found in /proc/sys/kernel on our systems.
cat /proc/sys/kernel/pid_max
32768 is fairly common on small systems with a common running process count under 1000. Generally, a system will incrementally assign ID until it hits the max and then wrap around and start assigning currently unused IDs. It is possible that large systems have a higher max set.
Process control is a complex issue and will be covered in detail in the processes module. However, it is helpful from the beginning to have a basic understanding of processes, jobs, and how to move them between the foreground and background.
The process ID can be used to change a process's running status from outside of the process. Note that users can only influence a process they have started and own.
This is a survey of some of the commands available in Unix/Linux for observing and influencing processes you have started.
ps - list processes running. Various options will affect the scope, which users' processes are listed, and amount of data about each process being displayed.
A list of some of options available. See the man page for more detail.
no options
-f
top - lists the top n (enough to fill the screen) processes running by resource usage. Updated every few seconds.
^z - <ctrl>z - control character that suspends current foreground process and places it in the background. It will display a summary :
[1]+ Stopped sleep 500showing the job ID in [], followed by a +, - or blank-space, status of background jobs, in this case 'Stopped', and the command being listed.
See jobs below for more info.
& - run command line command in background.
( sleep 500; echo awake )&How to interpret the command sequence above. The parens says to treat a sequence of commands as a set. The ampersand says to run the set in the background. And the semi-colon says to run the sleep 1st for 500 seconds and when done, echo the word awake.
Unlike ^z, commands issued with ampersand modifier will be placed in the background in a running state. The exception to this is if a command running in the background requires input from the keyboard. If this occurs, it will enter a stopped state.
bg [%#] - restart in the background a paused (stopped) background process. If multiple jobs exist, use % to target specific job number. Note that jobs that require input from the keyboard will return to a stopped condition.
fg [%#] - bring into the foreground a paused or running background process. If multiple jobs in background, use the job ID to pick target jobs.
jobs - lists background processes, both running and stopped in current
shell.
kill - sends a signal to a process, most often to terminate.
#( Running ps at the prompt lists user's processes started from the current
login session. This should list the command interpreter (bash) reading in the
commands you type and the ps command itself. It is possible that additional
processes are listed. )
#( Run top to see what processes are consuming the most resources. When top is
run in the foreground, the screen will update every 3 seconds. Enter q to
quit.)
top
job id in []
+, -, or space.
+ will be last job accessed in
Exercise :
ps
#( Add the full listing to see additional information. )
ps -f
#( List all processes on the system. Pipe it to less for easy viewing. )
ps -ef | less