Shell Programming: Running Two Applications Simultaneously

I have two applications that let me call them APP1 and APP2. I would like these two to run in parallel on my machine. It is not necessary that they start EXACTLY at the same time, but should start at about the same time. The initial thought was to have a shell script that looked like this:

./APP1 &
. / APP 2

Is this a trick or do I need to insert a wait statement to make sure that APP2 launches within a certain period of time?

thanks

+4
source share
5 answers

It could be better:

./app1 & ; ./app2 & 

But, as mentioned, the shell will start each of them as child processes in the sub-shell. Shell does not guarantee that there is any synchronization between processes or startup time.

Why do you need them to work in parallel? Perhaps understanding this requirement will give you a better answer.

You can create a very simple startup synchronization in two programs. Here is the example "app1" in the example.

 #!/bin/sh # app1.sh # Do any setup, open log files, check for resources, etc, etc... # Sync with the other app typeset -i timeout=120 count=0 touch /tmp/app1 while [[ ! -e /tmp/app2 ]] ; do if [[ $count -ge $timeout ]] ; then print -u2 "ERROR: Timeout waiting for app2" exit 1 fi (( count += 1 )) sleep 1 done # Do stuff here... # Clean up rm /tmp/app1 exit 0 
+5
source

Your solution should work in practice. Otherwise, you can use any scheduler, for example, at, cron, and similarly run both commands at a certain time.

+4
source

This will work fine.

+3
source

The AFAIK shell does not guarantee anything about the launch time of the programs, but in practice it should start almost at the same time.

+2
source

This will work, and you can even run APP2 to APP1. If the time is not important, but the order and APP1 MUST run before APP2, then this design will not give you this guarantee.

You should enable the sleep operator if you want to leave APP1 able to start before APP2 starts.

0
source

Source: https://habr.com/ru/post/1286683/


All Articles