Process control and access :

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

  • Only processes started under the current login shell will be listed.
    Information included in the listing : -u specific_user
  • Lists all processes associated with user running in any login on system. In this case, the ID of the sshd that is provided the login is also listed for each login instance.

  • Lists the same type of information as the plain ps

    -f

  • Full listing.
    Information included in the listing : -e
  • everything. Lists all processes currently running on the system. Displays same type of info as plain ps. Most commonly used with -f option so parent process also listed for each command.

    top - lists the top n (enough to fill the screen) processes running by resource usage. Updated every few seconds.

  • has a variety of options to tweak display, frequency of update, and modify output for use with other programs.

    ^z - <ctrl>z - control character that suspends current foreground process and places it in the background. It will display a summary :

    [1]+  Stopped                 sleep 500
    
    showing 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.

  • %# - # is the job number. Jobs are numbered sequentially from one in the current shell.

    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.

  • lists

    kill - sends a signal to a process, most often to terminate.

    Exercise :

    #( 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. )

    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
    

    #( 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

    Command list