Executing two commands in parallel

Hi,
I am stuck into a situation where i want to execute a command in my shell script well along with a previous command in order to achieve something but i am not figuring out a way.
here is a snippet:

service management restart
rm -rf lock.file

in the above, if you see, i am trying to restart a service first.
What happens is that as soon as that command of restart starts running, there is a temporary lock file created.
However, that lock file is causing some performance issues (the restart takes a long time because of the lock file).
Now, i want to override this event by deleting the lock file as soon as the restart command runs.

so, i am looking for way where i can delete the file as soon as the restart command starts running, which i am sure will solve my problem

Is there any way where i can run both commands in parallel (executing both commands together)
Please can any one guide me on this ?

I think you can run your first process in background by putting & at the end of the command and then run rm command.

1 Like

Thanks.
The '&' will put the job in background. but it does not move to the next command to execute until the existing job is complete.
so, in my case, the rm command will still wait until the job in the background completes.

No & does move the job to next line

1 Like

Thanks folks.
My bad. i dint try it. it works :slight_smile:

Thanks a lot.

ok .. now there is one more thing we might need to do while doing things in parallel.

suppose there 4 commands
cmd1,cmd2, cmd3, cmd4

goal is :
run cmd1 and cmd2 in parallel, once both are done move on to execute cmd3 and cmd4 in parallel, exit the script once both are done
answer:
_____________

cmd1 & 
pid1=$!

cmd2 & 
pid2=$!

wait $pid1
wait $pid2

cmd3 & 
pid3=$!

cmd4  & 
pid4=$!

wait $pid3
wait $pid4

_____________

1 Like