This is my task - pls help
Write a script that will run every 5 min and check if a particular file has appeared in a particular directory. Once it appears then rename the file and move it a bkp directory and run another script.
This is my task - pls help
Write a script that will run every 5 min and check if a particular file has appeared in a particular directory. Once it appears then rename the file and move it a bkp directory and run another script.
Well, it is your task, so what have you tried?
to run your script every 5 minutes, you need to schedule a cron job, see - http://www.unix.com/answers-frequently-asked-questions/13527-cron-crontab.html
the script would be something like:
file_to_check="/tmp/newfile"
backup_dir="/foo/"
another_script="/foo/bar"
if [ -e $file_to_check ] ; then
# Check if file exists
mv $file_to_check $file_to_check.backup # Rename the file
mv $file_to_check.backup $backup_dir # And move it to a backup dir
another_script # Run another script
fi
THis is my file named mycron which I add it to the crontab entry later to run every 5 mins and this works. But I havnt done the last part - to run another script if everything succeeds.
Thanks for the reply Yogesh. But in ur code, what if the mv fails - ie, say the destination directory doesnt exist or something else ... only after all this succeeds I need to start another script.
#
# Taking action if a particular file appears in a dir
#
filename="/home/mrudula/myfile"
count=`find "$filename" | wc -l`
if [ "$count" -eq 1 ]
then
\# Renaming filename
new_filename="$filename".new
mv $filename $new_filename
echo "$filename" renamed to "$new_filename"
\# Moving to backup directory
bkup_dir="$filename"-bckup
mkdir "$bkup_dir"
mv "$new_filename" "$bkup_dir/$\(basename $new_filename\)"
echo "$new_filename" moved to "$bkup_dir"
else
echo "No file found - "$filename""
fi