In Bash scripting, the “&” notation is utilized to run a command in the background. The “wait $!” command waits for the background command to complete. Overall, the “& wait $!” command is beneficial for running background processes and synchronizing their completion with other script parts.
This article will illustrate the significance of & wait $! Command with the practical implementation in Linux.
- What is the Significance of `& wait $!`?
- How Does `& wait $!` Work on Linux?
- Run Single Command
- Running Multiple Commands in Parallel
- Running a Command with a Timeout
What is the Significance of `& wait $!`?
The “&” is placed at the end of a script that tells the shell to execute the script in the background. This way, the script can continue running other commands without delaying the background command to end. Furthermore, the “$!” parameter goes to the PID of the last command that is run in the background. In the end, the “wait” command suspends the execution of the script until the background process terminates.
How Does `& wait $!` Work on Linux?
The notation ‘& wait $!’ does not have proper syntax but the combination of these notations is provided here in a practical way. Here is an example of how ‘& wait $!’ can be used:
Example 1: Run Single Command
An example is considered to run the single command in the background. For this, the bash script is written below:
#!/bin/bash # Start a command in the background ls & # Do some other work here echo "Working..." # Wait for the background command to complete wait $! # Background command has finished echo "Done."
In this example, the “ls” command is executed in the background using the “&” notation. The script then continues doing other work and printing “Working…”. After that, the “wait $!” command waits for “ls” to terminate before resuming with the script. Once the “ls” command has finished, the script prints “Done.”
Save and exit the nano editor.
To make the “script.sh” file executable, use the “chmod” command with the “x” option. After that, execute the “./script.sh” in the terminal as below:
$ sudo chmod +x script.sh $ ./script.sh
The output shows that the “ls” command has been executed in between the “Working…” and “Done” phrases.
Example 2: Running Multiple Commands in Parallel
To run the multiple commands in parallel in the background, a bash script is written as below:
#!/bin/bash # Start command1 in the background touch file.txt & # Start command2 in the background mkdir folder & # Wait for both commands to complete wait $! wait $! echo "All commands have finished."
In this bash script, two commands, “touch file.txt” and “mkdir folder,” are started in the background using the “&” notation. The “wait $!” command is utilized twice to wait for both commands to complete before printing “All commands have finished.”
Save and exit the script file in the nano text editor.
After that, use the “chmod” command with the “x” option by specifying the “script.sh” to make it executable and run in the terminal:
$ sudo chmod +x script.sh $ ./script.sh
The output shows that “All commands have finished” after executing the commands in the “script.sh” file.
Example 3: Running a Command with a Timeout
Another example is carried out to run the “ls” command by specifying the timeout and condition:
#!/bin/bash # Start command in the background ls & # Wait for a command to complete or timeout after 10 seconds sleep 10 && kill $! & wait $! if [ $? -eq 0 ]; then echo "Command completed successfully." else echo "Command timed out." fi
In the “script2.sh” file, the “ls” command is started in the background using the “&” notation. After that, the “sleep” command waits for 10 seconds before sending a kill signal to the process (kill $!) and waiting for it to complete (wait $!).
Furthermore, the “if” statement is utilized to check the exit code of the wait script. If the “ls” command is completed successfully, the script prints “Command completed successfully.” Otherwise, it prints “Command timed out.”.
Save and exit the text editor.
To execute the above file, use the “chmod” command with the “x” option by specifying the “script2.sh” file as below:
The output shows that “script2.sh” has been successfully executed.
In Linux, the ‘& wait $!’ notation is a beneficial tool for managing background processes. It allows scripts to run commands in parallel, wait for commands to complete, and perform actions based on the success or failure of commands. This is helpful when the script needs to synchronize the completion of multiple processes.
This guide has explained the significance of the `& wait $!` in Linux.