Control Implementation

Tutorial 4 of 4

Tutorial: Control Implementation in Shell Scripts

1. Introduction

Goal of the Tutorial

This tutorial aims to provide an understanding of control implementation in shell scripts. It will focus on how to pause, resume, or stop processes as needed, essential for efficiently handling script execution.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the concept of process control in shell scripting
- Implement commands to pause, resume, or stop processes
- Learn best practices for control implementation

Prerequisites

A basic understanding of shell scripting will be helpful. Familiarity with Linux/Unix commands is assumed.

2. Step-by-Step Guide

Control implementation in shell scripts involves the use of certain commands to manage the execution of processes.

  • Pause a process: The sleep command is used to pause a script for a specified time. It takes an argument that represents the number of seconds to sleep.
  • Resume a process: The fg command is used to bring a background process to the foreground, effectively resuming its operation.
  • Stop a process: The kill command is used to terminate a process.

Best Practices and Tips

  • Always ensure to handle processes in a controlled manner to prevent system resources from being overused.
  • Use the sleep command judiciously, as it can make your script slower if overused.
  • Be careful when using the kill command, as it can terminate critical processes if not used correctly.

3. Code Examples

Let's look at some examples.

Example 1: Using sleep command

echo "Start of script"
sleep 5   # pauses the script for 5 seconds
echo "End of script"

In this script, sleep 5 causes a pause of 5 seconds between the echoed messages. After running this script, you will first see "Start of script", then a pause of 5 seconds, followed by "End of script".

Example 2: Using fg command

To see fg in action, you need to have a process running in the background. Here is an example of how to do this:

# Start a process in the background
sleep 30 &

# Now, use `fg` to bring it to the foreground
fg

In this example, the sleep 30 & starts a sleep process in the background. When you run fg, it brings this background process to the foreground, effectively resuming its operation.

Example 3: Using kill command

# Start a process in the background
sleep 30 &

# Use `kill` to terminate it
kill $!

In this script, sleep 30 & starts a process in the background. The kill $! command terminates this process. $! is a special shell variable that contains the process ID of the most recently executed background pipeline.

4. Summary

In this tutorial, we've covered how to control the execution of processes in shell scripts. We've learned how to pause, resume, and stop processes using the sleep, fg, and kill commands, respectively.

5. Practice Exercises

Exercise 1: Write a script that displays the message "Starting process", sleeps for 5 seconds, and then displays "Process completed".

Exercise 2: Start a background process that sleeps for 60 seconds. Bring it to the foreground after 30 seconds.

Exercise 3: Start a background process that sleeps for 60 seconds. Terminate it before it completes.

For more practice, try to control more complex processes in your scripts. You can also explore other commands for process control such as bg, jobs, ps, top, etc. Happy scripting!