How to delay the process for few seconds

Hi,

In my shell script, (as per the requirement), I am creating few files, and the processes are launched parallelly . (by using "&" at the end of the command line). As per the logic, I need to remove these files as well, after creating.

But, the problem is, due to parallel processing, (while creation) if i am trying to remove the file, then it is not removing, because the previous process is in progress.

For this problem, I need to introduce some piece of code, (something like wait/sleep), which will give more time to complete these process and step for removing files won't be affected.

Ex-

(grep "pattern1" "file1" "file2" ;echo $? >> thread1.txt) &
(grep "pattern2" "file3" "file4" ;echo $? >> thread2.txt) &

------- Few lines of code ---------
rm -f thread1.txt thread2.txt

Hence, in between set of greps, and remove command, I need to use a piece of code, which will give some more time, to execute the grep to execute successfully. (can I use "sleep 1" over here ? or i need to use something else)

Please give some suggestion as this is very urgent for me. :slight_smile: Thanks for any kind of help.

Thanks and Regards,

Jitendriya Dash.

You can either let the script wait for the background processes, or sleep for a few seconds, or save the PIDs of the background processes ($!) and check ps -ef if they're still running.

You don't you use "predictive sleep" ? I mean, do you know approximately how long does it takes for the last process to finish ? If you give enough time, like 10 seconds or more, for example - sleep 10, would this be enough ? Then, get the status of the last command with $? and proceed based on the answer.

You can test the output from the shell "jobs" command. When there are no background jobs running it outputs nothing.