Automatically executing a command

Hi

I was wondering if anyone knew the answer to this question?

I am trying to find a way of executing a command if a certain file is created in the same directory.

One way I thought about doing this was to create a FORTRAN program that continually searches for this file. If the file exists then the FORTRAN program executes the command.

The problem with this is that I am worried that I will be utilising a whole CPU and am worried the CPU might go crazy!

So can anyone think of another way I could do this?
For example could I use a daemon that executes the command once the file exists? If so, how exactly can I set it up?

Or does anyone know how I could limit the CPU power for the FORTRAN program?
(I have tried the nice / renice commands but these will only limit the CPU power if all of the CPUs are being used.)

Thanks in advance

you can do it more simply with a script and using cron to run the script every few minutes.

Use the find command and -prune statement

find . -name "file" -prune {} \;

What type of command is it? Could you do it all in a shell script?

to forever look for the file in the directory, you can use ...

#! /bin/ksh

while true
do
    if [ -f /dir/file ]
    then
          echo "i found the file and it is here!"
    else
          sleep 1
    if
done

exit 0

or if you want the script to take a break or exit after it sees the file in the directory so you can run other scripts on it ...

#! /bin/ksh

while true
do
    if [ -f /dir/file ]
    then
          echo "i found the file and it is here!"
          break # replace with "exit" as required
    else
          sleep 1
    if
done

exit 0

Thanks for your reply.

Your idea sounds good however I would like to check whether the file is there or not more instantaneously. I don't think you can specify a time of less than a minute with CRON. Would you know of another way of doing this?

The command itself is just a batch script.

Look at Just Ice's solution above. The sleep command allows you to specify time in seconds, so that you can have the script running (almost) all the time without unneccessarily taxing your system.

the sleep line is set to 1 second which is the shortest you can set it to in ksh but i heard perl might be able to get you something less than a second ... the more advanced perl users here should be able to steer you in the right direction ...

Thanks all,

I took Just Ice's advice and made a csh script with a sleep of 1 second. It is way more elegant than the FORTRAN program however it would be excellent if the sleep command could use a value of less than 1 second (say 0.1 seconds). So if anyone has any ideas, please reply!?

To sleep for 1/10 of a second, try:

perl -e 'select(undef,undef,undef,.1)'

or similiar program in C ... nodoff sleep for less than a second

Thanks VERY much Perderabo.

Never used PERL before but quite impressed with your comment. I replaced the sleep 1" command with your perl line.

The script now "states" it is using 0.0% of the CPU resources which is great.

The final script (csh) I am now using is as follows:

#
set n = 0

while ( $n <= 10 )
if( -e ./hello) then
echo "Hello file is here"
rm hello
else
perl -e 'select(undef,undef,undef,.1)'
endif

end