Run multiple procedures from shell script parallely

Hi,
I need to write a Shell Script wherein i will connect to a DB2 Database and run multiple DB procedures. I know how to do in a way where procedures will be called one after the other, like when first procedure finishes, second will be executed.

But what i want is to run them at the same time like we execute background processes. Is there a way to achieve this.

Any help will be appreciated

Yes.

Run background processes from your shell script.

Hi,

I cannot place .sql files and run them in background. Below is my code

#! /bin/sh
. ~/.bash_profile
clear
db_user=$1
db_passwd=$2
db2 connect to RPTPRDD1 user $db_user using $db_passwd
#Use Multiple calls if needs to be executed for multiple input parameters.


db2 "Call PROC1"
db2 "Call PROC2"
db2 "Call PROC3"

#Close DB connection
db2 connect reset

I need these three procs to run in parallel, not one after the other.

After started bg process, save child process process id (variable !).

db2 "Call PROC1" &
pid1=$!

db2 "Call PROC2" &
pid2=$!

db2 "Call PROC3" &
pid3=$!


# then only wait that every process has done
wait $pid1 # wait process to end af it already ended then continue to the next line
wait $pid2 # so no need to make any while waiting 
wait $pid3

# or wait every together with one wait, wait exit after every process has ended.
wait $pid1 $pid2 $pid3
1 Like

Hi Kshji,

I tried this approach. Even though processes are created and i can see their PIDs. Still only one procedure is executing at a given time, the first one in sequence, followed by second and so on.
I believe only one procedure call is possible per DB Session.

Is there a way to make 3 seperate DB Sessions and run the procs one each in them.

This does create three separate sessions, by running db2 3 independent times.

If they are waiting for each other, there may be database locking involved and the like.

Or perhaps they are getting 'frozen' by attempting to interact with the user. They must be completely noninteractive to go in the background like this.

1 Like

To verify locking part, I ran the procedures in three seperate unix sessions. They ran perfectly. So it cant be locking.
You also mentioned frozen for interaction with user.
I also added nohup to the procedure call commands, still result is same.

When I check in database also, it shows only one procedure running from that user, others are not even started.

This wouldn't stop it from opening /dev/tty.

Please show the code you tried which didn't work. Maybe there's a mistake we can fix. There's no reason except the code or the terminal that it'd get frozen, 'command &' does exactly what you want - put it in background.

1 Like

Below is the code i used in my script:

   #! /bin/sh
clear
db2 connect to rptprdd1 user ss154253 using ss154253


nohup db2 "CALL PTL.GET_CASE_DETAIL(?,?,?)" &
pid1=$!
nohup db2 "CALL PTL.GET_CONSOLIDATED_DEMAND(?,?,?)" &
pid2=$!
nohup db2 "CALL PTL.GET_STATEMENT_DETAIL(?,?,?)" &
pid3=$!
nohup db2 "CALL PTL.GET_SURVEY_DETAIL(?,?,?)" &
pid4=$!
nohup db2 "CALL PTL.GET_TAN_COUNT(?,?,?)" &
pid5=$!
nohup db2 "CALL PTL.GET_TOTAL_COLLECTION(?,?,?)" &
pid6=$!


wait $pid1 $pid2 $pid3 $pid4 $pid5 $pid6


db2 connect reset




 

Its only running first procedure.
But when i run these 6 procs from six different unix sessions, all run simultaneously.

Do you have the setsid utility?

Hi,

I am not aware of this utility. I can only confirm after going back to office.

How can I check if I have it? Will it come up in man command.

Also how can i use it in my script.

whereis setsid or which setsid etc etc.