Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux shell script asynchronous commands and notification when completed

Tags:

linux

shell

I have a script which updates a web application. The web application is spread across 2 servers. Here is a rundown of the script

  1. The shell script updates the git repository.
  2. The shell script stops the application server.
  3. The shell script stops the web server.
  4. The shell script instructs the application server to checkout the latest git update.
  5. The shell script instructs the web server to checkout the latest git update.
  6. The shell script starts the application server.
  7. The shell script starts the web server.

Each of the 7 steps are done one after the other synchronously. The total run time is approximately 9 seconds. To reduce downtime however, many of these steps could be done asynchronously.

For example, step 4 and 5 could be done at the same time. I want to start step 4 and 5 asynchronously (e.g. running in the background), but I cannot find how to wait until they are both completed before going further.

like image 942
Anon21 Avatar asked Aug 30 '12 19:08

Anon21


People also ask

Does bash wait for command to finish before executing next?

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.

Is shell script asynchronous?

Bash isn't really asynchronous in the same way that JavaScript is asynchronous, however it can produce a result that would be similar to an asynchronous command in another language by forking.

What is synchronous and asynchronous execution of shell?

A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands.

How do you introduce a delay in shell script?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.


1 Answers

You might want to use command grouping to maintain which steps need to be synchronous:

step1
( step2 && step4 && step6 ) &
( step3 && step5 && step7 ) &
wait && echo "all done"
like image 84
glenn jackman Avatar answered Oct 16 '22 14:10

glenn jackman