Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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