Bookmark this page

Guided Exercise: Background and Foreground Processes

In this lab, students will start, suspend, and reconnect to multiple processes using job control.

Outcomes

Practice suspending and restarting user processes.

Log in as student to serverX. Begin in student's home directory.

  1. Open two terminal windows, side by side, to be referred to as left and right.

  2. In the left window, start a process that continuously appends the word "rock" and a space to the file ~/outfile at one-second intervals. The complete command set must be contained in parentheses for job control to interpret the set as a single job.

    [student@serverX ~]$ (while true; do echo -n "rock " >> ~/outfile; sleep 1; done)
  3. In the right window, use tail to confirm that the new process is writing to the file.

    [student@serverX ~]$ tail -f ~/outfile
  4. In the left window, suspend the running process. The shell returns the job ID in square brackets. In the right window, confirm that the process output has stopped.

    [student@serverX ~]$ Ctrl+z
  5. In the left window, view the jobs list. The + denotes the current job. Restart the job in the background. In the right window, confirm that the process output is again active.

    [student@serverX ~]$ jobs
    [1]+  Stopped                 ( while true; do
        echo -n "rock " >> ~/outfile; sleep 1;
    done )
    [student@serverX ~]$ bg
    [student@serverX ~]$ jobs
  6. In the left window, start two more processes to append to the same output file. Replace "rock" with "paper," and then with "scissors." To properly background the process, the complete command set must be contained in parentheses and ended with an ampersand.

    [student@serverX ~]$ (while true; do echo -n "paper " >> ~/outfile; sleep 1; done) &
    [student@serverX ~]$ (while true; do echo -n "scissors " >> ~/outfile; sleep 1; done) &
  7. In the left window, view jobs to see all three processes "Running". In the right window, confirm that all three processes are appending to the file.

    [student@serverX ~]$ jobs
  8. Using only commands previously learned, suspend the "rock" process. In the left window, foreground the job, using the job ID determined from the jobs listing, then suspend it using Ctrl+z. Confirm that the "rock" process is "Stopped". In the right window, confirm that "rock" output is no longer active.

    [student@serverX ~]$ jobs
    [student@serverX ~]$ fg %number
    [student@serverX ~]$ Ctrl+z
  9. End the "paper" process. In the left window, foreground the job, then terminate it using Ctrl+c. Confirm that the "paper" process has disappeared. In the right window, confirm that "paper" output is no longer active.

    [student@serverX ~]$ jobs
    [student@serverX ~]$ fg %number
    [student@serverX ~]$ Ctrl+c
  10. In the left window, view the remaining jobs using ps. The suspended job has state T. The other background job is sleeping (S), since ps is "on cpu" (R) while displaying.

    [student@serverX ~]$ ps j
     PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
     4489  6223  6223  6223 pts/1    12918 Ss    1000   0:00 bash
     4489  6237  6237  6237 pts/2     9782 Ss    1000   0:00 bash
     6237  9782  9782  6237 pts/2     9782 S+    1000   0:00 tail -f /home/student/o
     7360  9856  7360  6223 pts/1    12918 T     1000   0:00 sleep 1
     7395 12916  7395  6223 pts/1    12918 S     1000   0:00 sleep 1
     6223 12918 12918  6223 pts/1    12918 R+    1000   0:00 ps j
  11. Stop the remaining two jobs. In the left window, foreground either job. Terminate it using Ctrl+c. Repeat with the remaining job. The "Stopped" job temporarily restarts when foregrounded. Confirm that no jobs remain and that output has stopped.

    [student@serverX ~]$ fg %number
    [student@serverX ~]$ Ctrl+c
    [student@serverX ~]$ fg %number
    [student@serverX ~]$ Ctrl+c
    [student@serverX ~]$ jobs
  12. In the right window, stop the tail command. Close extra terminal windows.

    [student@serverX ~]$ Ctrl+c
Revision: rh124-7-1b00421