Shell Script to Demonstrate Wait Command in Linux

02 Jun, 2022
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit.
ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution of the calling thread until one of its children terminate. It will return the exit status of that command. The sleep command is used to delay the execution of the next command for a given number of seconds, hours, minutes, days. kill is used to terminate a background running process. It sends a terminate signal to the process and then processes halts. It takes process ID as an argument. exit command is used to exit from the current shell environment.
Let’s take some examples to understand more about wait commands:
wait is an inbuilt command in the Linux shell. It waits for the process to change its state i.e. it waits for any running process to complete and returns the exit status.
Syntax:
wait [ID]
Here, ID is a PID (Process Identifier) which is unique for each running process. To find the process ID of a process you can use the command:
pidof [process_name]
Example: Finding PID of VLC (video player)
Finding PID of process
Here, VLC is the process name. And 3868 is its Process ID.
Creating a simple process.
Using a special variable($!) to find the PID(process ID) for that particular process.
Print the process ID.
Using wait command with process ID as an argument to wait until the process finishes.
After the process is finished printing process ID with its exit status.
Note: Exist status 0 indicates that the process is executed successfully without any issue. Any value other than 0 indicates that the process has not been completed successfully.
Script:
#!/bin/bash

# creating simple process that will create file and write into it
cat > GEEKSFORGEEKS.txt <<< "Something is going to save into this file" &

# this will store the process id of the running process
# $! is a special variable in bash 
# that will hold the PID of the last active process i.e. creating a file.
pid=$!

# print process is running with its PID
echo "Process with PID $pid is running"

# Waiting until process is Completed
wait $pid

# print process id with its Exit status
# $? is special variable that holds the return value of the recently executed command.
echo "Process with PID $pid has finished with Exit status: $?"
Output:
Using wait command
Here, we are creating a process that will create a file with a given string and store it into the storage device. Then using a special variable “$!” we will store the PID of that process into another variable pid. Then we are displaying the process ID and using the wait command to wait until the process is completed. After the process has been executed we are returning the process exit status code and its PID. If the Exit status code is 0 then we can say that the process is executed successfully.
Approach:
Creating multiple processes.Creating a text file and writing into it.Sleep 2 will pause the shell for 2 seconds.Again creating a text file and writing into it.
Creating a text file and writing into it.
Sleep 2 will pause the shell for 2 seconds.
Again creating a text file and writing into it.
Display that we are running multiple processes.
Using wait -f to wait until all the above process has been executed successfully.
After the process is finished we are printing its exit status using a special variable $?.
Script:
#!/bin/bash

# creating multiple processes
cat > Geeks.txt <<< "GeeksGeeks" &
sleep 2 &
cat > something_new.txt <<< "Something new is created" &

echo "Multiple processes are running"

# using wait to wait for the processes to finish
# -f will wait until all the processes is executed.
wait -f

#printing process Exit status
# $? is special variable that holds the return value of the recently executed command.
echo "All Processes end with Exit status: $?"
Output:
Using wait command
Here, we are creating 3 processes. Then we simply use the wait command to wait for all processes to be finished. -f is used to wait for all processes to be finished before returning the exit code. After that, we simply print the exit status of these processes using a special variable “$?”
akshaysingh98088
anikakapoor
varshagumber28
nikhatkhan11
Picked
Shell Script
How To
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.