In this exercise, you use signals to manage and stop processes.
Outcomes
Start and stop multiple shell 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-kill
Instructions
On the workstation machine, open two terminal windows side by side.
In this section, these terminals are referred to as left and right.
In each terminal, use the ssh command to log in to the servera machine as the student user.
[student@workstation ~]$ ssh student@servera
[student@servera ~]$In the left terminal shell, create the /home/student/bin directory.
Create the instance shell script in the new directory.
Change the script permissions so that it is executable.
Create the /home/student/bin directory.
[student@servera ~]$ mkdir /home/student/binCreate the instance script file in the /home/student/bin directory.
Press the i key to enter Vim interactive mode.
The file must have the following content as shown.
Use the :wq command to save the file.
[student@servera ~]$cd /home/student/bin[student@servera bin]$vim /home/student/bin/instance#!/bin/bash while true; do echo -n "$@ " >> ~/instance_outfile sleep 5 done
The instance script runs until the process is terminated.
It appends command-line arguments to the ~/instance_outfile file once every 5 seconds.
Make the instance script file executable.
[student@servera ~]$ chmod +x /home/student/bin/instanceIn the left terminal shell, change into the /home/student/bin/ directory.
Start three processes with the instance script file, by passing the network, interface, and connection arguments.
Start the processes in the background.
[student@servera bin]$instance network &[1] 3460 [student@servera bin]$instance interface &[2] 3482 [student@servera bin]$instance connection &[3] 3516
In the right terminal shell, verify that all three processes are appending content to the /home/student/instance_outfile file.
[student@servera ~]$ tail -f ~/instance_outfile
network interface network connection interface network connection interface network
...output omitted...In the left terminal shell, list existing jobs.
[student@servera bin]$ jobs
[1] Running instance network &
[2]- Running instance interface &
[3]+ Running instance connection &Use signals to suspend the instance network process.
Verify that the instance network process is set to Stopped.
Verify that the network process is no longer appending content to the ~/instance_output file.
Stop the instance network process.
Verify that the process is stopped.
[student@servera bin]$kill -SIGSTOP %1[1]+ Stopped instance network [student@servera bin]$jobs[1]+ Stopped instance network [2] Running instance interface & [3]- Running instance connection &
In the right terminal shell, view the tail command output.
Verify that the word network is no longer appended to the ~/instance_outfile file.
...output omitted...
interface connection interface connection interface connection interfaceIn the left terminal shell, terminate the instance interface process.
Verify that the instance interface process disappeared.
Verify that the instance interface process output is no longer appended to the ~/instance_outfile file.
Terminate the instance interface process.
Verify that the process is terminated.
[student@servera bin]$kill -SIGTERM %2[student@servera bin]$jobs[1]+ Stopped instance network [2] Terminated instance interface [3]- Running instance connection &
In the right terminal shell, view the tail command output.
Verify that the word interface is no longer appended to the ~/instance_outfile file.
...output omitted...
connection connection connection connection connection connection connection connectionIn the left terminal shell, resume the instance network process.
Verify that the instance network process is set to Running.
Verify that the instance network process output is appended to the ~/instance_outfile file.
Resume the instance network process.
Verify that the process is in the Running state.
[student@servera bin]$kill -SIGCONT %1[student@servera bin]$jobs[1]+ Running instance network & [3]- Running instance connection &
In the right terminal shell, view the tail command output.
Verify that the word network is appended to the ~/instance_outfile file.
...output omitted...
network connection network connection network connection network connection network connectionIn the left terminal shell, terminate the remaining two jobs. Verify that no jobs remain and that output is stopped.
Terminate the instance network process.
Next, terminate the instance connection process.
[student@servera bin]$kill -SIGTERM %1[student@servera bin]$kill -SIGTERM %3[1]+ Terminated instance network [student@servera bin]$jobs[3]+ Terminated instance connection
In the left terminal shell, list the current running processes in all open terminal shells.
Terminate the tail processes.
Verify that the processes are no longer running.
List all current running processes.
Refine the search to view only tail lines.
[student@servera bin]$ ps -ef | grep tail
student 4581 31358 0 10:02 pts/0 00:00:00 tail -f instance_outfile
student 4869 2252 0 10:33 pts/1 00:00:00 grep --color=auto tailTerminate the tail process.
Verify that the process is no longer running.
[student@servera bin]$pkill -SIGTERM tail[student@servera bin]$ps -ef | grep tailstudent 4874 2252 0 10:36 pts/1 00:00:00 grep --color=auto tail
In the right terminal shell, verify that the tail command is no longer running.
...output omitted...
network connection network connection network connection Terminated
[student@servera ~]$Close the extra terminal.
Return to the workstation system as the student user.
[student@servera bin]$ exit
logout
Connection to servera closed.
[student@workstation ~]$This concludes the section.