help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to set up a repeat check procedure?


From: lina
Subject: Re: [Help-bash] how to set up a repeat check procedure?
Date: Fri, 30 Dec 2011 22:11:41 +0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16

On Friday 30,December,2011 09:14 PM, Greg Wooledge wrote:
On Thu, Dec 29, 2011 at 12:55:15PM +0100, Jasper Noë wrote:
On Thursday 29,December,2011 03:29 PM, lina wrote:
How can I let a script keep on checking whether a file exist or not?
Generally you make a loop like this:

while true; do

        something

done

In this particular case you could write:

until [ -f $file ]; do

        sleep 60

done
Using a loop that keeps looking for something, over and over, with some
heuristically chosen delay between checks, is called "polling".  It's
the most primitive way to achieve the stated goal.  It's also the only
portable way.

The disadvantages of polling for a file are that it's horribly inefficient
(we keep burning CPU cycles on false checks, and on forking sleep(1)
processes), and when a file is finally present, there could be a delay
until the next loop iteration finally wakes up to find it.  In the
example given above, there could be 60 seconds (or more) between the
time the file is opened, and the time it is found.

In the case of a file that is created non-atomically (e.g. by opening
it, writing stuff to it, writing more stuff to it, and finally closing
it), it gets even worse: the polling loop might wake up and discover
the file while it is only partially written.  If the polling loop is
responsible for triggering some sort of processing of the contents of
that file, that could be disastrous.

But in order to address that, we would have to understand more about what
the *real* goal is.  Are you checking for files that are uploaded to some
Real case, it's in cluster, I want to check some job finished or not, and then run following script, which needs the final file generated from former simulations.

$ more ../autorun.sh
#!/bin/bash

while [ 'qstat | grep REM | grep R' ];
    do sleep 10;
done

./rem_extend.sh

Last time I left the computer to sleep, it's supposed to run the following script in 2 hours,
but morning I found it's not.  kinda of totally sleep over?

sort of shared directory, via FTP or a PHP script or SFTP or something?
(If so, you're doomed....)

Anyway, if you simply want to get rid of the inefficiency and the delay,
and if you don't have to worry about the contents of the file (e.g.
simply its very existence is the trigger for your processing), then
you might look into OS-dependent solutions such as FAM or inotifywait.

Or, perhaps your goal is "I want to tell my script to wake up and do
stuff, and I'm using this file as a signal", in which case you should
look at actual signals instead.  Shell scripts can set up signal handlers
using "trap".

I will read "trap" soon,

Thanks,
Best regards,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]