In this exercise, you will start, suspend, background, and foreground multiple processes using job control.
Outcomes
You should be able to use job control to suspend and restart user processes.
Log in to workstation as student using student as the password.
On workstation, run the lab processes-control start command.
This script ensures that servera is available.
[student@workstation ~]$lab processes-control start
On workstation, open two terminal windows side by side.
In this section, these two terminals are referred to as left and right.
In each terminal, use the ssh command to log in to servera as the student user.
[student@workstation ~]$ssh student@servera...output omitted... [student@servera ~]$
In the left window, create a new directory called /home/student/bin.
In the new directory, create a shell script called control.
Make the script executable.
Use the mkdir command to create a new directory called /home/student/bin.
[student@servera ~]$mkdir /home/student/bin
Use the vim command to create a script called control in the /home/student/bin directory.
To enter Vim interactive mode, press the i key.
Use the :wq command to save the file.
[student@servera ~]$vim /home/student/bin/control#!/bin/bash while true; do echo -n "$@ " >> ~/control_outfile sleep 1 done
The control script runs until terminated.
It appends command-line arguments to the file ~/control_outfile once per second.
Use the chmod command to make the control file executable.
[student@servera ~]$chmod +x /home/student/bin/control
Execute the control script.
The script continuously appends the word "technical" and a space to the file ~/control_outfile at one second intervals.
You are able to execute your control script because it is located in your PATH, and has been made executable.
[student@servera ~]$control technical
In the right terminal shell, use the tail command with the -f option to confirm that the new process is writing to the /home/student/control_outfile file.
[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 window, confirm that the process output has stopped.
^Z [1]+ Stopped control technical [student@servera ~]$
technical technical technical technical
...no further output...
In the left terminal shell, view the jobs list.
Remember that the + sign indicates the default job.
Restart the job in the background.
In the right terminal shell, confirm that the process output is again active.
Using the jobs command, view the list of jobs.
[student@servera ~]$jobs[1]+ Stopped control technical
Using the bg command, restart the control job in the background.
[student@servera ~]$bg[1]+ control technical &
Use the jobs command to confirm that the control job is running again.
[student@servera ~]$jobs[1]+ Running control technical &
In the right terminal shell, confirm that the tail command is producing output.
...output omitted...
technical technical technical technical technical technical technical technical
In the left terminal shell, start two more control processes to append to the ~/output file.
Use the ampersand (&) to start the processes in the background.
Replace technical with documents and then with database.
Replacing the arguments helps to differentiate between the three processes.
[student@servera ~]$control documents &[2] 6579 [student@servera ~]$[student@servera ~]$control database &[3] 6654
The job number of each new process is printed in square brackets. The second number is the unique system-wide process ID number (PID) for the process.
In the left terminal shell, use the jobs command to view the three running processes. In the right terminal shell, confirm 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 technical process. Confirm that it has been suspended. Terminate the control documents process and confirm that it has been terminated.
In the left terminal shell, use the fg command with the job ID to foreground the control technical process. Press Ctrl+z to suspend the process. Use the jobs command to confirm 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, confirm that the control technical process is no longer sending output.
database documents database documents database
...no further output... In the left terminal shell, use the fg command with the job ID to foreground the control documents process. Press Ctrl+c to terminate the process. Use the jobs command to confirm 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, confirm that the control documents process is no longer sending output.
...output omitted... database database database database database database database database ...no further output...
In the left window, use the ps command with the jT option to view the remaining jobs.
The suspended jobs have a state of T.
The other background jobs are sleeping (S).
[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 jT
In the left window, use the jobs command to view the current jobs. Terminate the control database process and confirm that it has been terminated.
[student@servera ~]$jobs[1]+ Stopped control technical [3]- Running control database &
Use the fg command with the job ID to foreground the control database process. Press Ctrl+c to terminate the process. Use the jobs command to confirm that the process is terminated.
[student@servera ~]$fg %3control database ^C[student@servera ~]$jobs[1]+ Stopped control technical
In the right terminal shell, use the Ctrl+c command to stop the tail command.
Using the rm command, delete the ~/control_outfile file.
...output omitted... Ctrl+c[student@servera ~]$rm ~/control_outfile
Log out from servera on both terminals.
[student@servera ~]$exitlogout Connection to servera closed.
[student@servera ~]$exitlogout Connection to servera closed.
This concludes the guided exercise.