0

I have loop in shell script and it takes time process only a few files in the list. How do I push the process in the background and select the next file while the first file is still being processed?

   for newfile in `ls new*`
    do
    time consuming process &
    done

I tried & at the end of the command after > /dev/null 2>&1 but it does not seem to work.

2

To put a process in background just put the & at the end and remove the ; you used to terminate the command before done

for newfile in new* ; do
    long_running_command 2>&1 > /dev/null &
done

You don't need the

`ls new*`

you can just use

new* 

the shell will expand it for you.

Example

touch new1
touch new2
for newfile in new* ; do ls $newfile 2>&1 > /dev/null & done

produces

[1] 92197
[1]+  Done                    ls $newfile 2>&1 > /dev/null
[1] 92198
[1]+  Done                    ls $newfile 2>&1 > /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.