[Solved] Running scripts in parallel

i have script A and script B, both scripts have to run in parallel, my requirement is script A create table temp1, post creating it will run fr 4 hrs , script B has to start 0nly after creation of table temp1 ( which is done by script A) , again script B will run for 5 hrs

if i run sequencially it will run for 9 hrs, how can i run in parallel( but above condition should match) ..........

You could have different scripts for the two tasks and run one (or both) in background. You will still have to use some sort of inter-process-communication to make sure the one starts only when the second is past a certain point of execution. The following (pseudo-code-) sketch would layout this:

Master-script:

rm /some/file     # make sure there is no leftover from previous runs
first_script &
while [ ! -e /some/file ] ; do
      sleep some time
done
second_script


first_script:
long_command       # commands to be executed before second_script starts
touch /some/file
other_commands     # commands to be executed after second_script is started

This would first start "first_script" and then wait until "/some/file" is created, upon which it starts "second_script". "first_script" would signal being past a certain point by creating the file "/some/file", upon which "second_script" would be started by "master".

Another option would be to use Korn shell ("ksh") and make use of its "coprocess facility". I suggest you read the man page for "ksh" on how to use it.

I hope this helps.

bakunin

1 Like

What I am about to say assumes that table temp1 is a file.

If it is then:

  1. At the end of script B ensure that temp1 is either deleted or renamed so that it doesn't exist anymore.

  2. At the start of script B, "test" (see man pages) to see if the file temp1 exists and if it doesn't then "sleep" (see man pages) for, say, 600 seconds to sleep the script. Only when temp1 exists will script B continue processing.

The above also assumes that temp1 is created fairly instantly so will either exist or not exist. If temp1 is created slowly, then get script A "touch" (see man pages) a file to use as a flag to script B which can then "test" for the existance of the flag file instead. Script B should delete the flag file before it finishes.

Hope that helps (and I have succeeded in making that clear enough).

Post any further questions.

1 Like

One option is to move table creation step in script A to a new script then make both script A and B dependent on this new script and fire off in parallel.

Or, you could update script B to check for the existence of the table and if it does not exist yet have it sleep for some time and only continue once the table has been created.

1 Like

Just invoke script_B right after creation of table: temp1 inside script_A:-

script_A

#!/bin/ksh

# Some code here

sqlplus -s ${user}@${pass}@${inst} << EOF
create table temp1 as select ... ;
exit
EOF

script_B & 

# Some code here
1 Like
 
scriptA.sh
touch letsgotempfile_$(date +'%Y%m%d%')
 
$ more scriptB.sh
#!/usr/bin/ksh
log_path='/data/mig04/logs'
timestamp=$(date +'%Y%m%d%')
while [ ! -f  /letsgotempfile_$(date +'%Y%m%d%') ]
do
sleep  5
echo " sleeping for 5 sec .............."
done
echo " Going to start B ........."
exit 0



above code is working !!!!!................

i can not invoke scriptB in script A because for both scripts i am passing diffrent parameters

---------- Post updated at 09:46 PM ---------- Previous update was at 04:04 PM ----------

thanks everyone for ur valuable inputs ........script is working fine :slight_smile:

My only comment would be to ask why you sleep only for 5 seconds?

You indicated previously that both scripts take several hours to run. Having one waking up and burning CPU cycles every 5 seconds might just be wasteful of CPU. Why don't you sleep for 10 minutes each time.

yes, i understand .....in script A, the creation of table tempa will take 1 to 10min ....anyways.... thank you :slight_smile: