arsenal
arsenal

Reputation: 24184

Check every half and hour and see whether the if condition is true or not

There are two directories, which contains some files. I am working with Hadoop and both of these two directories are Hadoop File System.

First Location of Directory-

/sys/edw/

Second Location of Directory-

/apps/only/

I want to see if these two directories has some files or not. If they contains some files, then continue and execute my other shell scripts, but if they do not contain any files, wait for half an hour and then check back again whether the files has arrived in those two directories or not.

If it has arrived then execute other shell scripts but if it is not arrived then wait for half an hour again and keep on checking every half an hour.

Algorithm-

If [ hadoop fs -ls /sys/edw/   AND hadoop fs -ls /apps/only/ ]
then
continue executing my other shell scripts
but if condition is not true
wait for half an hour and then check again with the if loop and keep on looking every half an hour until the if condition is not true

I want to execute this below script only when the above if condition is true.

query.sh

Basically I have few scripts that I want to run only if condition is true otherwise keep on checking every half an hour and see whether the if condition is true or not.

I am runnign SunOS 5.1

Updated Code:-

I am trying something like below and file doesn't exist in this directory(/apps/hdmi/20120916), so it should be sleeping for half an hour but instead of sleeping, it goes to execute the other shell scripts which it shouldn't be doing-

while [ ! hadoop fs -ls /apps/hdmi/20120916 ]; do
    sleep 1800
done

echo "Hello new11"
pwd

Anything wrong I am doing?

Updated One Again:-

while ! hadoop fs -ls /apps/hdmi/20120916 ; do
    sleep 1800
done

echo "Hello new11"
pwd

I have tried the above one also, but this directory doesn't exist (/apps/hdmi/20120916) and instead of sleeping for half an hour, it goes and print the Hello new11 which shouldn't be happening. It should wait for half an hour. Anything else I am missing? I am running SunOS 5.1

Upvotes: 0

Views: 1884

Answers (3)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

You can't use while ! COMMAND; do. ! is not a negation operator in this context. what you want to do is execute the command hadoop fs -ls /DIR and sleep when the command doesn't list any files.

For executing the command put it between backticks ``.

`hadoop fs -ls /apps/hdmi`

Use test or square brackets [ ] to evaluate the output of the command.

[ `hadoop fs -ls /apps/hdmi` ]

The expression will evaluate to true if the command produces a non-zero string (i.e. when there are files in the directory). Since that's the opposite of what you want (continue while there are no files), you need to negate the expression.

[ ! `hadoop fs -ls /apps/hdmi` ]

Thus your script should look somewhat like this:

while [ ! `hadoop fs -ls /apps/hdmi` ]; do
  sleep 1800
done

echo "Hello new11"

pwd

This is assuming that hadoop fs -ls does not produce any output when there is no file in the directory.

Upvotes: 1

knittl
knittl

Reputation: 265707

If you don't want to use cron, then you can use an infinite loop:

while true; do
  if hadoop fs -ls /apps/hdmi/20120916; then
    query.sh # call other script
    break # exit loop
  else
    sleep 1800
  fi
done

I hope I understood your requirements correctly.

Upvotes: 2

nneonneo
nneonneo

Reputation: 179677

If you want your script to quit once the condition becomes true, just use sleep 1800 to sleep the script for 1800 seconds (half an hour), and a while loop around that:

while [ ! condition ]; do
    sleep 1800
done

# execute other scripts since condition is now true

Upvotes: 0

Related Questions