I have used the & command before to make a script run another script in the background like so:
#!/bin/bash
echo "Hello World"
script1.sh &
script2.sh &
echo "Please wait..."
But lets say I have another script with an IF ELSE statment and I would like to set an ELIF statement mid flow as a background task with the & and then carry on with processing the rest of my script knowing that while rest of the ELIF will carry running in the back ground:
#!/bin/bash
if cond1; then
stuff
sleep 10 &
stuff
stuff
elif cond2; then
something else
else
echo "foo"
fi
stuff
echo "Hello World"
I really hope this makes sense any help would be greatly appreciated.
You can run your commands in a subshell. A subshell will execute in a separate (child) process. You can define which commands are ran in a subshell by surrounding them with parentheses.
And, as you know, to run a process in the background just append an
&.Here is what your
ifstatement would look like if you were to execute it in a separate, background, process: