Let's assume we have a scenario to write a shell script, where need to execute some statements in parallel, after finishing those need to execute few more statements.
for example if i have a shell script,
cmd1 &
cmd2 &
cmd3
i want to execute cmd3 after finishing cmd1 and cmd2.
This scenario may look simple, but it is not.
When i run statements in parallel using & operator, then that statement will be executed in background, so the other set of statements will be executed immediately, these will not wait for background processes.
i have solved this problem with the below script,
i am running one infinite loop, there i am tracking all the background processes, if all the background processes are done, then only i am running the final statements.
cmd1 &
cmd2 &
while true
do
if [ `jobs | grep Running | wc -l` -eq 0 ]; then
cmd 3
break;
fi;
done
for example if i have a shell script,
cmd1 &
cmd2 &
cmd3
i want to execute cmd3 after finishing cmd1 and cmd2.
This scenario may look simple, but it is not.
When i run statements in parallel using & operator, then that statement will be executed in background, so the other set of statements will be executed immediately, these will not wait for background processes.
i have solved this problem with the below script,
i am running one infinite loop, there i am tracking all the background processes, if all the background processes are done, then only i am running the final statements.
cmd1 &
cmd2 &
while true
do
if [ `jobs | grep Running | wc -l` -eq 0 ]; then
cmd 3
break;
fi;
done
No comments:
Post a Comment