Welcome to this tutorial on process handling in shell scripts. The goal of this tutorial is to help you understand how to manage and control processes within your shell scripts, which is a key aspect of scripting that allows for efficient resource management and task execution.
By the end of this tutorial, you will learn:
Prerequisites
Before starting this tutorial, you should have a basic understanding of shell scripting. Knowledge of basic programming concepts like variables, loops, and conditionals will also be beneficial.
In computing, a process is simply an instance of a program in execution. Whenever you run a script, it becomes a process that your operating system manages.
You can start a process in a shell script by simply writing the command that you want to run. For example:
#!/bin/bash
echo "Starting process..."
ls
In this example, ls
is the process that we are starting. This script will output the contents of the current directory.
You can monitor your processes using the ps
command. It provides information about the currently running processes, including their process identification numbers (PIDs).
#!/bin/bash
echo "Starting process..."
ls
echo "Current processes:"
ps
You can stop a process using the kill
command followed by the PID of the process.
#!/bin/bash
echo "Starting process..."
sleep 60 &
echo "Current processes:"
ps
echo "Killing process..."
kill $!
In this example, we start a process that will sleep for 60 seconds. We then kill this process before it completes. The $!
variable contains the PID of the last background process.
#!/bin/bash
echo "Running a long process in the background..."
sleep 60 &
echo "The script continues without waiting!"
In this example, we run a process that sleeps for 60 seconds. By adding the &
at the end of the command, we are telling the script to run this process in the background, allowing the script to continue without waiting for the process to complete.
#!/bin/bash
echo "Running a long process..."
sleep 60 &
echo "Waiting for the process to complete..."
wait $!
echo "The process has completed!"
In this example, we run a process in the background and then use the wait
command to pause the script until the process completes.
In this tutorial, we've learned how to handle processes in shell scripts. We've covered how to start, monitor, and stop processes, as well as how to run processes in the background and wait for them to complete.
For further learning, you can explore more advanced process handling techniques, such as handling process signals and managing process priorities.
Write a script that starts two processes in the background and waits for them both to complete.
Write a script that starts a process in the background, waits for 5 seconds, and then kills the process.
#!/bin/bash
echo "Starting two processes..."
sleep 60 &
ls &
echo "Waiting for processes to complete..."
wait
echo "Processes have completed!"
#!/bin/bash
echo "Starting a process..."
sleep 60 &
echo "Waiting 5 seconds..."
sleep 5
echo "Killing process..."
kill $!
In the first exercise, you've started two processes and used wait
without a PID, which will wait for all background processes to complete. In the second exercise, you've learned to kill a process after a certain amount of time.