As the doctor gone rogue

September 16, 2011

waiting….

Filed under: bash — hypotheses @ 1:06 am

I recently have been working on several projects that involve waiting for a file to be created first before I can proceed to the next step. One option for doing this is to monitor a process ID of the submitted script and see if it finished or not. However, the previous job might finished with an error and the correct output file might not have been created. So, I came up with a solution to create a file that would contain the output that I can check that the previous job ran correctly. Let’s call this file “JOB.WELLDONE”.

Then, I will have a script running in background to monitor if the “JOB.WELLDONE” exists. Then continue doing what I plan to do.


waiting() {
FILE=$1
TIME=$2
COUNT=0
while [[ ! -e $FILE ]]; do
echo -n -e "\r $COUNT : Waiting for $FILE"
sleep $TIME;
let "COUNT=COUNT+1"
done
echo "$FILE found. Proceed!!! "
}

Then, I include waiting function in the top of all these shell script. Or a better option will be making this a separate command in your ~/bin directory so you can call it again and again.

Here’s an example.

#!/bin/sh
waiting() {
FILE=$1
TIME=$2
COUNT=0
while [[ ! -e $FILE ]]; do
echo -n -e "\r $COUNT : Waiting for $FILE"
sleep $TIME;
let "COUNT=COUNT+1"
done
echo "$FILE found. Proceed!!! "
}

## Wait for JOB.WELLDONE and recheck every 60 seconds

waiting JOB.WELLDONE 60

CONTINUE WITH WHAT YOU WANT TO DO
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: