Search code examples
bashshellprocessjobs

How to run a second command which depend on the finish of the first command when the first command will suspend


I have a situation, image I have a first command to run which starts a db server, in the end of this command, it will suspend and listening on some port. And I have the second command which is to start some web server to connect to the database from the first command.

Now I don't know how to write this simple command, tried:

A ; B

which doesn't work, because when A goes into suspend, b doesn't execute, if I type ctrl+c, then B starts, but it doesn't make sense anymore, because A is already terminated, so there's no database process.

I tried:

A && B

It doesn't work neither, B doesn't execute because A doesn't exit successful.

I thought of using:

A & B

which doesn't work sometimes, because when A is put into background, B immediately starts, so sometimes, B trying to connect to a database port when A hasn't finished, so there's no database process yet.

How can I achieve that B only starts when A successfully starts database?


Solution

  • A little broad, but I guess it gets the point across

    A&
    while [ some_test_fails ]; do
        sleep some_time
    done
    B