Sas
Sas

Reputation: 278

Run Shell script parallely

I do create scripts and give it to production team. and the production team have to run all these scripts everyday. say if there are 100 scripts they have to run nohup command for particular date 100 times.

example

nohup abc.sh 2018-03-01 &
nohup abc1.sh 2018-03-01 &
nohup abc2.sh 2018-03-01 & and so on till the last script.

now i have done the following Wrapper

echo "*** Start time ---> `date '+%Y-%m-%d %H:%M:%S'` ***" 
for date in `cat datefile_Prepaid_data` 
do 
date1=`echo $date|cut -d"|" -f1`
echo $date1
sh abc.sh $date1
sh abc1.sh $date1
sh abc2.sh $date1 and so on till the last script

return_code=$? 
if [ ${return_code} -eq 0 ] 
then 
echo "\n  Success... for date $date " 
else 
echo "\n   Failed... for date $date .. So exiting." 
exit 1 
fi 
done 

but the issue is this wrapper run the scripts one by one. Do we have another approach to achieve this and also the scripts should run parallel.

I have created a separate file 'datefile_Prepaid_data' which is having the dates for which the wrapper should run.

Upvotes: 1

Views: 65

Answers (1)

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

Hi If you see from the script you are not running them in parallel. To run them parallel you have to run each job in background. Like below:-

sh abc.sh $date1 &
sh abc1.sh $date1 &
sh abc2.sh $date1 & 
#and so on till the last script

But one issue here is that it will execute script from return_code=$? onward immediately. Hence you have to put a wait command so that it should wait till last job completes.

 wait
 #now below commend will execute only when all jobs completes.
 #also it will wait till all job completes successfully
 return_code=$? 

Now one more problem here. Your $? will provide you the exit status of the last job completed. If that did not run successfully your below logic will be execute incorrectly. So you have to modify below condition based on current requirement.

 if [ ${return_code} -eq 0 ] 

To know whether scripts were executed successfully or not just add below scripts in every job:-

In all abc.sh to abc99.sh

 echo "Job $0 completed with exit status :$? >> job_status.log

Now a complete answer for you:-

echo "*** Start time ---> `date '+%Y-%m-%d %H:%M:%S'` ***" 
for date in `cat datefile_Prepaid_data` 
do 
    date1=`echo $date|cut -d"|" -f1`
    echo $date1
    sh abc.sh $date1 &
    sh abc1.sh $date1 &
    sh abc2.sh $date1 &
        #and so on till the last script
done 

In all abc.sh to abc99.sh

 echo "Job $0 completed with exit status :$? >> job_status.log

Upvotes: 1

Related Questions