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...
#!/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
Umm, are you sure you need a script for that
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)
#!/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...
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).
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
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