Linux – Shell Script to Start and Kill Processes Sequentially

bashlinuxshell-scriptUbuntu

Let's imagine I have a client and a server on the same machine, and I'd like to script some interaction between them.

I would really like a shell script to –

  1. Start server
  2. Put server in the background
  3. Start client
  4. (wait for client to do whatever it does)
  5. Stop server

I can do most of that already, like this –

./server &
./client

But that leaves server running after the script finishes which, apart from anything else, is very untidy.

What can I do?

Best Answer

You can use bash job control:

#!/bin/bash

./server &
./client
kill %1

Be sure to put the #!/bin/bash at the beginning of the script so that bash is used to execute the script (I'm not sure if job control is supported in sh, please correct me if it does).

Related Question