Automatic script trigger

Hi,

I'm looking for a way to solve the following scenario:

A shell should automatically trigger / run when a text file is placed or present at a specific location.

My idea - to create a cron / anacron for every minute and inside that i will call a temp script. Temp script will move to my required path and using a for loop i will check for files in that directory and execute my script.

Eg:

cd /a/b/c
for i in *
{
  sh core_script.sh $i
}

Is this correct? and is there any other ways i can achieve this.

share your suggestions.

If you are on a Linux based platform, use inotify. It can be set up to work exactly as you describe - wait for a new file to appear then exit, used inside a loop.

Thanks Jim.

In case of unix is there any feature for this.

You're giving us confusing and inadequate information to really help you.

You posted a message to the Linux and UNIX Forums asking how to do something to files in directory \a\b\c , but that is a Windows style path; not a UNIX and Linux stye path.

You were given a suggestion that might work on Linux systems; and then you tell us you want a solution for UNIX systems. What you're asking for is not something that is covered by the standards (so if there is a way to do it, it may vary considerably from platform to platform).

What UNIX system are you using? Which release of that UNIX system are you using?

If you're using a recent Solaris or Mac OS X system, you might be able to use dtrace (although doing so may require extended privileges). (See the dtrace(1M) man page or the Oracle Solaris Dynamic Tracing Guide for details.)

If you're using some other UNIX system, it might also have dtrace or some other facility that will allow you to monitor changes to a directory in general, creation of files in a directory, or even creation of files whose name ends with .txt in a specific directory.

Giving details about your environment when asking a question makes it much more likely that you will get answers that work for you.

I'm sorry Don, as i'm new here not aware of other platforms.

It is Solaris 5.10 version.

In destination path we won't create any .txt files .. it would be an incoming file to that path and which will trigger a script

Huh? How does "an incoming file" get to that path if a file isn't created with that path? You can use dtrace to track link() and symlink() as well as open() with O_CREAT set (AKA creat() ) if you mean that the file is created or copied elsewhere and linked into this directory when it is complete.

1 Like

Incoming File - like why can't it be moved or copied from some other path.

So only when i CREATE a file in that specific path i would be able to use dtrace .

Any way to accomplish if the scenario is in this manner?.

Do Something like below

cd /your_path
while true; do
        if [ -f your_file.txt ]; then
                break
        else
                sleep 60
                get_data_files
        fi
done
get_data_files()
{
echo "Copying your_file.txt file from ${WINDIR}"
ftp -in $ftp_server << EOF > ~/log.txt 2>&1
user $user_name $pass_word
cd $dir
get your_file.txt
bye
}

Missing EOF .

get_data_files()
{
echo "Copying your_file.txt file from ${WINDIR}"
ftp -in $ftp_server << EOF > ~/log.txt 2>&1
user $user_name $pass_word
cd $dir
get your_file.txt
bye
EOF
}

And I think the functions must be defined before calling them.

The cp utility will have to use creat() (or open(...O_CREAT) ) to create the file it is copying to. The mv will have to do the same thing cp does if it is a move across filesystem boundaries, or use link() to create a name for the moved file in the target directory (and unlink() to remove the old name from the source directory) if the move is within a filesystem. All of these can be trapped with dtrace .

1 Like