Objectives
Upon completion of this unit, you should be able to:
  • Explain what a process is
  • Describe how to manage process
  • Use job control tools
What is a Process?
1) A process is a set of instructions loaded into memory
  • Numeric Process ID (PID) used for identification
  • UID, GID and SELinux content determines filesystem access. Normally inherited from the executing user
Listing Processes
1) View Process information with ps
  • Shows the processes from the current terminal by default
  • -a includes processes on all terminals
  • -x includes processed not attached to terminals
  • -u prints process owner information
  • -f prints process parentage
  • -o PROPERTY, … prints custom information: pid, comm, %cpu, %mem, state, tty, euser, ruser
Finding Processes
1) Most flexible: ps options | other commands
  • ps axo comm, tty | grep ttyS0
2) By predefined patterns: pgrep
  • pgrep –U root
  • pgrep –G student
3) By exact program name: pidof
  • pidof bash
Signals
1) Most fundamental inter-process communication
  • Sent directly to processes, no user-interface required
  • Programs associate actions with each signal
  • Signals are specified by name or number when sent:
        - Signal 15, TERM (default) – Terminate clearly
        - Signal 9, KILL – Terminate immediately
        - Signal 1, HUP – Re-read configuration files
        - man 7 signal shows complete list
Sending Signals to Processes
1) By PID: kill [signal] pid …
2) By Name: killall [signal] comm …
3) By Pattern: pkill [-signal] pattern
Scheduling Priority
1) Scheduling priority determines access to the CPU
2) Priority is affected by a process’s nice value
3) Value range from –20 to 19 but default to 0. Lower nice value means high CPU priority
4) Viewed with ps –o comm, nice
Altering Scheduling Priority
1) Nice values may be altered…
  • when starting a process: $ nice –n 5 command
  • After starting: $ renice 5 PID
2) Only root may decrease nice values
Interactive Process Management Tools
1) CLI: top
2) GUI: gnome-system-monitor
3) Capabilities:
  • Display real time processed information
  • Allow sorting, killing and re-nicing
Job Control
1) Run a process in the background: Append an ampersand to the command line: firefox &
2) Temporarily halt a running program: Use Ctrl+z or send signal 17 (STOP)
3) Manage background or suspended jobs
  • List job numbers and names: jobs
  • Resume in the background: bg [%jobnum]
  • Resume in the foreground: fg [%jobnum]
  • Send a signal: kill [-SIGNAL] [%jobnum]
Scheduling a Process To Execute Later
1) One-time job use at, recurring jobs use crontab
Create at time crontab -e
List at –l crontab -l
Details at –c jobnum N/A
Remove at –d jobnum crontab –r
Edit N/A crontab –e
2) Non-redirected output is mailed to the user
3) root can modify job for other users
Crontabe File Format
1) Entry consists of five space-delimited fields followed by a command line. One entry one line, no limit to line length
2) Fields are minute, hour, day of month, month, and day of week
3) Comment lines begin with #
4) See man 5 crontab for details
Grouping Commands
1) Two way to group commands:
  • Compound: date; who | wc –l (commands run back-to-back)
2) Subshell: (date; who | wc –l) >> /tmp/trace
  • All output is sent to a single STDOUT and STDERR
Exit Status
1) Processes report success or failure with an exit status
  • 0 for success, 1-255 for failure
  • $? stores the exit status of the most recent command
  • exit [num] terminates and set status to num
Conditional Execution Operators
1) Commands can be run conditionally based on exit status
  • && reprents conditional AND THEN (the second runs only when the first succeeds)
  • || reprents conditional OR ELSE (the second runs only when the first fails)
The test Command
1) Evaluates boolean statements for use in conditional execution
  • Return 0 for true
  • Reture 1 for false
2) Examples in long form:
$ test “$A” = "$B” && echo “Strings are equal”
$ test “$A” -eq "$B” && echo “Integers are equal”
3) Examples in shorthand notation
$ [ “$A” = "$B” ] && echo “Strings are equal”
$ [ “$A” -eq "$B”] && echo “Integers are equal”
File Tests
1) File tests:
  • -f tests to see if a file exists and is a regular file
  • -d tests to see if a file exists and is a regular directory
  • -x tests to see if a file exists and is a regular executable
Scripting: if Statements
1) Execute instructions based on the exit status of a command
if ...; then …
elif …; then …
else …
fi
End of Unit11
1) Questions and Answers
2) Summary:
  • A process is any set of instructions in memory
  • Processes are managed with: ps, kill, top, gnome-system-monitor
  • Suspend jobs with Ctrl+z, manage with fg, bg