A Developer's Diary

Apr 3, 2008

Shell Scripting - Parallel Process Execution in Background

Command for checking the background jobs which are in running state

jobs -r | wc -l


Non-interactive ftp transfer to automate uploading of files parallely on different platforms.

#!/bin/bash

LINUX=linux
AIX=aix
SUNOS=sunos

WIN32=win

MACHINES=" \
    $LINUX \
    $AIX \

    $SUNOS \
    $WIN32"

function checkjobs(){
if [ `jobs -r | wc -l` -eq 0 ]; then

echo "All Background Process completed Successfully"
return 0
fi
sleep 10

checkjobs
}

function upload(){

if [ $# -eq 0 ]; then

echo "Missing Argument"
exit 1
fi

# Non-interactive ftp transfer
ftp -i -v -n hostname <<END_FTP

    user root root
    binary
    cd test
    put "file$1.exe"
    bye

END_FTP
echo "Uploaded file$1.exe"
return 0
}

function uploadfile(){
echo "Uploading Files  Started"
for host in $MACHINES

do
echo "Uploading FILES on host $host"
upload $host &
done

}

uploadfile
checkjobs
echo "All Files Uploaded Successfully"





Making things more simpler, we can use the special command wait and rewrite uploadfile so that we need not check for the background processes

function uploadfile(){
echo "Uploading Files  Started"

for host in $MACHINES
do
echo "Uploading FILES on host $host"

upload $host &
done
wait
}

uploadfile
echo "All Files Uploaded Successfully"




No comments :

Post a Comment