Red Hat System Administration I
Use job control to start, suspend, and move multiple processes to the background and foreground.
Outcomes
Use job control to suspend and restart user processes.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start processes-control
Instructions
On the
workstationmachine, open two terminal windows side by side. In this section, these two terminals are referred to as left and right. In each terminal, log in to theserveramachine as thestudentuser.[student@workstation ~]$
ssh student@servera[student@servera ~]$In the left terminal shell, create the
/home/student/bindirectory. Create a shell script calledcontrolin the/home/student/bindirectory. Change the script permissions to make it executable.Create the
/home/student/bindirectory.[student@servera ~]$
mkdir /home/student/binCreate a script called
controlin the/home/student/bindirectory. To enter Vim interactive mode, press the i key. Use the:wqcommand to save the file and quit.[student@servera ~]$
vim /home/student/bin/control#!/bin/bash while true; do echo -n "$@ " >> ~/control_outfile sleep 1 doneNote
The control script runs until the process is terminated. The script appends command-line arguments to the
~/control_outfilefile once per second.Make the
controlfile executable.[student@servera ~]$
chmod +x /home/student/bin/control
Execute the
controlscript. The script continuously appends the word "technical" and a space to the~/control_outfilefile at one-second intervals.[student@servera ~]$
control technicalIn the right terminal shell, verify that the new process is writing to the
/home/student/control_outfilefile.[student@servera ~]$
tail -f ~/control_outfiletechnical technical technical technical ...output omitted...In the left terminal shell, press Ctrl+z to suspend the running process. The shell returns the job ID in square brackets. In the right terminal shell, confirm that the process output is stopped.
^Z[1]+ Stopped control technical [student@servera ~]$technical technical technical technical
...no further output...In the left terminal shell, view the
jobscommand output. Remember that the+sign indicates the default job. Restart the job in the background. In the right terminal shell, verify that the process output is again active.View the list of jobs.
[student@servera ~]$
jobs[1]+ Stopped control technicalRestart the
controljob in the background.[student@servera ~]$
bg[1]+ control technical &Verify that the
controljob is running again.[student@servera ~]$
jobs[1]+ Running control technical &In the right terminal shell, confirm that the
tailcommand is producing output....output omitted... technical technical technical technical technical technical technical technical
In the left terminal shell, start two more
controlprocesses to append to the~/outputfile. Use the ampersand (&) special command to start the processes in the background. Replacetechnicalwithdocumentsand then withdatabase. Replacing the arguments helps to differentiate between the three processes.[student@servera ~]$
control documents &[2] 6579 [student@servera ~]$control database &[3] 6654In the left terminal shell, use the
jobscommand to view the three running processes. In the right terminal shell, verify that all three processes are appending to the file.[student@servera ~]$
jobs[1] Running control technical & [2]- Running control documents & [3]+ Running control database &...output omitted... technical documents database technical documents database technical documents database technical documents database ...output omitted...
Suspend the
control technicalprocess. Confirm that it is suspended. Terminate thecontrol documentsprocess and verify that it is terminated.In the left terminal shell, foreground the
control technicalprocess. Press Ctrl+z to suspend the process. Verify that the process is suspended.[student@servera ~]$
fg %1control technical^Z[1]+ Stopped control technical [student@servera ~]$jobs[1]+ Stopped control technical [2] Running control documents & [3]- Running control database &In the right terminal shell, verify that the
control technicalprocess is no longer sending output.database documents database documents database
...no further output...In the left terminal shell, run the
control documentsprocess in the foreground. Press Ctrl+c to terminate the process. Verify that the process is terminated.[student@servera ~]$
fg %2control documents ^C[student@servera ~]$jobs[1]+ Stopped control technical [3]- Running control database &In the right terminal shell, verify that the
control documentsprocess is no longer sending output....output omitted... database database database database database database database database
...no further output...
In the left terminal shell, view the remaining jobs. The suspended jobs have a state of
T. The other background jobs are sleeping and have a state ofS.[student@servera ~]$
ps jTPPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 27277 27278 27278 27278 pts/1 28702 Ss 1000 0:00 -bash 27278 28234 28234 27278 pts/1 28702 T 1000 0:00 /bin/bash /home/student/bin/control technical 27278 28251 28251 27278 pts/1 28702 S 1000 0:00 /bin/bash /home/student/bin/control database 28234 28316 28234 27278 pts/1 28702 T 1000 0:00 sleep 1 28251 28701 28251 27278 pts/1 28702 S 1000 0:00 sleep 1 27278 28702 28702 27278 pts/1 28702 R+ 1000 0:00 ps jTIn the left terminal shell, view the current jobs. Terminate the
control databaseprocess and verify that it is terminated.[student@servera ~]$
jobs[1]+ Stopped control technical [3]- Running control database &Use the
fgcommand with the job ID to run thecontrol databaseprocess in the foreground. Press Ctrl+c to terminate the process. Verify that the process is terminated.[student@servera ~]$
fg %3control database ^C[student@servera ~]$jobs[1]+ Stopped control technicalIn the right terminal shell, use the Ctrl+c command to stop the
tailcommand. Delete the~/control_outfilefile....output omitted...
^C[student@servera ~]$rm ~/control_outfileClose the extra terminal. Return to the
workstationsystem as thestudentuser.[student@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$