How to automated the shell script

hye there... really need ur help...
i have a file ./filename...
then i want to make ./filename automatic run...
for example:
if someone send me a file using scp...then the ./filename will run automatically...

did u guys get what i mean....
if not please ask me...
cz i really need ur help ASAP....

For that purpose you must have a script running in background that checks periodically if the event you want to trap has occured. Therefore, we need more details :
What's the event

isn't precise enough. Else, the structure will be something like

#!/bin/bash
while true
do
    # Check if file sent or received....
    if Received
    then ./filename
    fi
    sleep 10 # for 10 seconds
done

Another question : should ./filename be run only once or any time the event occurs ?

thanks for the reply...
what i mean is....
assume that there are pc A and pc B...
inside pc A i create anne.txt...
i will send anne.txt using scp to pc B...
inside pc B..I already create filename.sh...
when anne.txt received at pc B...
then filename.sh will running automatically...

everytime pc A send new anne.txt i want it replaced the older ann.txt...
really need ur help...

It can then be done by checking the modified time

#!/bin/bash
F="anne.txt"
M1=$(stat -c %y "$F") # Initialize to avoid running at startup
while true
do
    M2=$(stat -c %y "$F")
    if [ "$M2" != "$M1" ]
    then
        ./filename.sh && M1=$M2 # Update variable only if filename.sh succeed (exit 0)
    fi
    sleep 10 # for 10 seconds
done
1 Like

Just opening more possibilities for you. There is a type of cron job that will monitor for file changes. you can look for incron for more details.

thnks for the reply...
i already tried cronjob...
but it does not work....

---------- Post updated at 08:20 AM ---------- Previous update was at 03:07 AM ----------

please.....help me...
im stuck....:frowning:

Umm, are you sure you need a script for that :confused:
Afaik the new file automatically will overwrite the existing file?

I'll try to illustrate that:

There is no file anne.txt in directoryX on PCB
PCA -> scp -> PCB -> directoryX -> anne.txt
PCA -> scp -> PCB -> directoryX -> anne.txt (the previous anne.txt gets overwritten with this new one)

yup...
that what im trying to do...

it will occur everytime the event happen...
thnks 4 d quick reply...

All right, now I got it :rolleyes:

Check frans' posting #4 :b:

#!/bin/bash
F="anne.txt"
M1=$(stat -c %y "$F") # Initialize to avoid running at startup
while true
do
    M2=$(stat -c %y "$F")
    if [ "$M2" != "$M1" ]
    then
        ./filename.sh && M1=$M2 # Update variable only if filename.sh succeed (exit 0)
    fi
    sleep 10 # for 10 seconds
done

i dont understand what M1 and M2 mean....
could u please help me..
explain this further...
thank you so much...:confused:

M1 is a variable, which will store the output of the command "stat -c %y "$F", namely the time of last modification. The same applies to M2. Then both are compared in the if statement, if they differ, the script filename.sh gets executed and the value of M1 becomes value of M2, else this script will wait 10 seconds and do the comparison again (and again, and again).

thnks for the details....
ok...
i create filename.sh and anne.txt in different folder...

when i send the anne.txt...
nothing appear...
so..how should i know that ./filename.sh is running

did you add "exit 0" in your' code in the if condition?

nope...
i juz modify the path of both file (anne.txt and filename.sh)
but still does not work...

For the beginning, create 2 scripts in the folder which contains anne.txt.

First script, let's say - monitor.sh - will contain frans' code.
The second, let's say - filename.sh - will do whatever you wanted to do, this is not specified by now. For the beginning put following 2 lines in it:

#!/bin/bash
echo "File anne.txt CHANGED!!

Now run monitor.sh.
When you overwrite anne.txt, in the next few seconds a message should appear on the terminal saying "File anne.txt CHANGED!!".

If it works, you can put both scripts whereever you want, but you need to adjust the path to filename.sh and anne.txt in the monitor.sh file.

when i run ./monitor...
this error will occured
stat: cannot stat `anne.txt': No such file or directory
stat: cannot stat `anne.txt': No such file or directory

Does the file anne.txt exist? Is it in the same directory as monitor?

yup...
it is in the same directory...
i create one directory.. that have monitor.sh, filename.sh and anne.txt

now...what should i do...
really need ur help
:frowning:

What is the output of ls -l in that directory?
Also include the output of cat monitor.sh

---------- Post updated at 18:20 ---------- Previous update was at 18:18 ----------

Post the full command that you run, not ...

this is the output in the directory:
-rw-r--r-- 1 autonimst autonimst 537426 2010-05-31 23:22 anne.txt
-rwxrwxrwx 1 autonimst autonimst 42 2010-05-31 23:53 filename.sh
-rwxrwxrwx 1 autonimst autonimst 325 2010-05-31 23:55 monitor.sh

and this is the monitor.sh output when i run ./monitor.sh:
#!/bin/bash
cd /home/autonimst/Desktop/yahnessus/
F="anne.txt"
M1=$(stat -c %y "$F") # Initialize to avoid running at startup
while true
do
M2=$(stat -c %y "$F")
if [ "$M2" != "$M1" ]
then
./filename && M1=$M2 # Update variable only if filename.sh succeed (e$
fi
sleep 10 # for 10 seconds
done