In this tutorial, we’ll learn how to start a bash script after another one finishes in Linux. This can be useful if we want to queue jobs one after another.
2. Starting Another Script in the Same Shell
If the script is run in the current shell, we can easily start another one after it.
For this tutorial, we’ll be using two example scripts located at /home/baeldung/script1.sh and /home/baeldung/script2.sh respectively. They just sleep for some amount of time and print a message:
$ cat ./script1.sh echo "Sleeping for 60 seconds..." sleep 60 echo "Slept for 60 seconds" $ cat ./script2.sh echo "Sleeping for 45 seconds..." sleep 45 echo "Slept for 45 seconds"
2.1. Using the && Operator
The bash && operator executes another command after the first one exits. The syntax for it’s usage is command1 && command2 && …:
$ ./script1.sh && ./script2.sh Sleeping for 60 seconds... Slept for 60 seconds Sleeping for 45 seconds... Slept for 45 seconds
We should note that the && operator executes the second command only if the first one returns an exit code of 0.
If we want to run the next script, even if the first one fails, we can replace the && operator with the; operator, which runs the next command regardless of the exit code.
2.2. Using wait
We can launch a script in the background initially and later wait for it to finish before executing another script using the wait command.
We use the single & operator to launch background processes:
$ ./script1.sh & $ Sleeping for 60 seconds... $ wait && ./script2.sh Slept for 60 seconds Sleeping for 45 seconds... Slept for 45 seconds + Done ./script1.sh
This command works even if the process exits with a non-zero failure code.
Moreover, we can cancel the wait command with the Ctrl + C key combination:
$ ./script1.sh & $ Sleeping for 60 seconds... $ wait && ./script2.sh # Cancel wait ^C $ wait && ./script2.sh # Restart wait Slept for 60 seconds Sleeping for 45 seconds... Slept for 45 seconds + Done ./script1.sh
This approach allows us to do other work in the same shell, but we must type wait && command2 again in case we want to wait again for the process to finish.
2.3. Using fg
If a command is already running in the background, we can bring it to the foreground with the fg command:
$ ./script1.sh & Sleeping for 60 seconds... $ fg; ./script2.sh ./script1.sh Slept for 60 seconds Sleeping for 45 seconds... Slept for 45 seconds
If it is already the foreground process, then we can suspend it with the Ctrl + Z key combination and then run the next script with fg as well:
$ ./script1.sh Sleeping for 60 seconds... ^Z+ Stopped ./script1.sh $ # We suspended the process, now we can use fg. $ fg; ./script2.sh ./script1.sh Slept for 60 seconds Sleeping for 45 seconds... Slept for 45 seconds
fg blocks until the command finishes, hence we can use the ; operator to run the next script.
We should note that fg cannot be canceled and resumed like wait since fg gives back control of the script to us instead of waiting for the script to finish. So all actions apply to the script itself.
3. Starting Another Script Outside the Shell
Sometimes we might not have access to the original shell where the script was launched. This usually happens when we daemonize a script from another shell and then close the terminal. In such cases, we can periodically poll for the script’s process ID and start the other script once the process ID no longer exists.
We can use pgrep to find the process ID of a script by passing in the script name as an argument:
$ pgrep script1 7775
Now, we can periodically check if this process ID has finished execution and then run our script:
pid=7775 # An entry in /proc means that the process is still running. while [ -d "/proc/$pid" ]; do sleep 1 done ./script2.sh
We should note that this method will not immediately run the script after the first one finishes as we sleep for 1 second before polling again. If more accuracy is required, we can reduce the sleep to a lower amount, such as sleep 0.1 for 100ms.
In this article, we learned how to run scripts one after the other in Linux using bash commands under various conditions.