4

I want run all script from the directory . Like ,

The directory contains 40 script,i would like to run first 5 script parallel.after completing these scripts the next 5 script will be execute as well as the remaining.

Please give any solutions by using linux and perl commands

6 Answers 6

10

Everyone loves to reinvent parallel execution tools.

Sign up to request clarification or add additional context in comments.

Comments

2

Can you use nohup for the first 5 scripts? Have the 5th script write to some file that it has completed and continue onwards.

Comments

1

When you are in the folder with the scripts run this bash script/command:

for var in $(ls *.pl)
do
    $var &
    # 5 simultaneous jobs 
    while test "$(jobs |wc -l)" -gt 4 
    do
        sleep 1
    done
done

this relies on you not having other background jobs running, test this by writing "jobs" on the commandline.

1 Comment

I have later learned that you can just use xargs to run multiple parallel jobs. To gzip files simultaneously with 4 processes: ls *.pl |xargs -n1 -P4 gzip
1

Don't forget GNU Make! Use a makefile like this and run with the -j option. See http://www.gnu.org/software/make/manual/make.html#Parallel

scripts=$(wildcard *.sh)
all: $(patsubst %.sh,%.out,$(scripts))
%.out: %.sh
    sh $< > $@ 2>&1

If you were working in Perl I'd suggest Parallel::ForkManager

Oh, and it seems that xargs on Linux has a -P option to run jobs in parallel. I haven't used it as my GNU Make trick predates it.

Comments

0
You can write sth like below. It's pseudo code.

#!/bin/ksh

dir=$1
count=0
for script in $1/*
  do
   count++
   $i &
   add the pid to pidList array
   if( count==5){
     while(waitforProcessComplete(pidList){
     }
   }

  done

Comments

0

I'd prefer GNU parallel. It's easy and fun - and the documentation comes with many neat examples.

The big advantage: You don't have to write any code or Makefile to do run your scripts. Example:

# create the example scripts:
mkdir scripts
for i in {1..50}; do {echo '#!/bin/sh'; echo "echo $i"; echo "sleep 1" } > scripts/s$i.sh; done

# use parallel to run all these scripts, 5 at a time:
parallel -j 5 ::: scripts/*.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.