LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2012, 03:59 PM   #1
shdobxr
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
running script every 1 second... mv'ing files once ctime = 5 seconds older than epoch


Hello all, I have used this forum for quite sometime, but never joined. Today is that day. Looking through the forums I have yet to find exactly what gets the following done.

Cliff notes....

When a file arrives in /home/user/Music/inbound the script waits to move the file to a destination when the ctime (in epoch) is 5 seconds old. This allows for the file to not be busy during it's transit to it's final destination.

The older work around made the file stale for 1 min due to find not being able to get down to atime or ctime in seconds. (ala hpux `ls -e`)

#####old script
find /home/user/Music/inbound -type f -cmin +01 -print | while read i; do
dirname=`dirname "$i"`;
filename=`basename "$i"`;
destname="$dirname/$filename";
mv "$i" "$destname";
mv "$destname" /home/outbound; done
#####old script


##############New effort
#!/bin/bash
###
EPOCHDATE=`date +%s`
FILECOUNT=`ls /home/user/Music/inbound | wc -l`
EPOCHFILELIST=`ls -l /home/user/Music/inbound --time=ctime --time-style=+%s | awk '{print $6;}'| head -2`
EPOCHSTALEFILENAME=`ls -l /home/userdMusic/inbound --time=ctime --time-style=+%s | awk '{print $7;}'| head -2`
EPOCHSTALEFILEMOVE=`ls -l /home/user/Music/inbound --time=ctime --time-style=+%s | egrep "INSERT FILES YOU DON:T WANT"| awk '{print $7;}'| head -2`
OUTCOME=$((EPOCHDATE-5))
EPOCHSTALEFILENAME=`ls -l /home/user/Music/inbound --time=ctime --time-style=+%s | egrep -v "INSERT FILES YOU DON:T WANT"| awk '{print $7;}'| head -2 | tr -d '\n'`
EPOCHSTALEFILENUMBER=`ls -l /home/user/Music/inbound | egrep -v "INSERT FILES YOU DON:T WANT"| awk '{print $7;}'| head -2 | tr -d '\n' | wc -w`


TERMINATORFILE="/var/run/backgroundworker.ctl"
GOON=1
while [ $GOON ]; do
[ -f "$TERMINATORFILE" ] && GOON=0
# do your repeated stuff below
cd /home/userd/Music/inbound;
if [ "$EPOCHSTALEFILENUMBER" -eq 0 ] || [ "$EPOCHSTALEFILENUMBER" -eq : ]; then
sleep 1
echo "nothing here"
elif [ "$EPOCHFILELIST" -lt "$OUTCOME" ] && [ "$EPOCHSTALEFILENUMBER" -gt 0 ]; then
mv $EPOCHSTALEFILENAME /tmp ;
#
# else
# if [ "$EPOCHSTALEFILENUMBER" -eq 0 ] || [ "$EPOCHSTALEFILENUMBER" -eq : ]; then
# echo "nothing here"
# fi
fi
done
sleep 1
done
rm -f "$TERMINATORFILE"
############### end new effort
 
Old 02-15-2012, 04:18 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

welcome to LQ.

My question would be:
what are you trying to do ?
 
Old 02-15-2012, 04:20 PM   #3
PenGUiN_6_1
Member
 
Registered: Oct 2006
Location: Brunswick, MD
Distribution: gNewSense 2.3 (deltah) i386
Posts: 57

Rep: Reputation: 22
THIS MAY NOT WORK ON ALL SYSTEMS...BE WARNED:
Code:
#!/bin/bash
# OK, let me get your files from the place in question
find $PlaceInQuestion -name "*.*" >> /tmp/PlaceInQuestion.totalstuff
# Now that I have THAT out of the way...
cat /tmp/PlaceInQuestion.totalstuff | grep $sexp >> /tmp/StuffIwant
# So, I now have the stuff I want, so...
for FileIWant in `cat stuffIWant` do ;
# FIRST, copy...
cp $FileIWant $LocationIWant
# THEN, delete (if so desired)...
rm $FileIWant
hopefully, that gives the system the ability to gather all of the names, then go through and copy them to another place, and delete them, from the original place (ignore the delete part if so desired)

Hopefully this helps, as find can generate even dot files; if this does not work, play with it until it does, or dismiss it ($sexp is the stuff you would use with grep; for more specifics, you should play around with it). Sorry if this does not help
 
Old 02-15-2012, 05:17 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by shdobxr View Post
When a file arrives (..) the script waits to move the file to a destination when the ctime (in epoch) is 5 seconds old. This allows for the file to not be busy during it's transit to it's final destination.
If that is your primary concern then the assumption that "5 seconds is enough" (reminiscent of a certain somebody who said "640K of memory should be enough for anybody." ;-p) may be wrong. If you instead use inotify and trigger a move on CLOSE_WRITE you've successfully eliminated that pitfall.
 
Old 02-15-2012, 06:00 PM   #5
shdobxr
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for all of the quick responses to show interest. Means a lot. I have been working on this script for quite some time.

@ PenGUiN_6_1

What does $sexp actually do?

Quote:
Originally Posted by lithos View Post
Hi,

welcome to LQ.

My question would be:
what are you trying to do ?
Ok, files will appear in the /home/user/Music/inbound. This directory is just for testing. In reality there is no music involved here. Just 1gig urandom files that I put into there to test my script.

As a large file is being written to /home/user/Music/inbound I would like writing to that file to be complete before I move it to it's final destination. In the first script I was using find to get down to the core file name I am looking for, then adding -cmin 1 to see those files whose change time (man stat) is older than one minute. This glued up script has been working. However it creates lag of 1 min before that file is actioned. I needed to move these files faster. Say 5 seconds after the writing to the file has been accomplished. So I am using the ls command is formatting ctime to output into epoch time. This way I can create conditions to ls for files within a certain time frame and action them accordingly.

Does this make more sense? I do not want to action (move a file) until it's epoch time is 5 seconds old.
 
Old 02-15-2012, 08:56 PM   #6
PenGUiN_6_1
Member
 
Registered: Oct 2006
Location: Brunswick, MD
Distribution: gNewSense 2.3 (deltah) i386
Posts: 57

Rep: Reputation: 22
I am sorry; $sexp was just my way of saying 'put whatever parameters you need when using grep'; it is not supposed to be a variable (I was unsure of how to express that). Again, sorry for the confusion
 
Old 02-16-2012, 02:49 AM   #7
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by shdobxr View Post
Thanks for all of the quick responses to show interest. Means a lot. I have been working on this script for quite some time.

...
Ok, files will appear in the /home/user/Music/inbound. This directory is just for testing. In reality there is no music involved here. Just 1gig urandom files that I put into there to test my script.

As a large file is being written to /home/user/Music/inbound I would like writing to that file to be complete before I move it to it's final destination.
...
I needed to move these files faster. Say 5 seconds after the writing to the file has been accomplished.
this really sounds like you should use inotify , inotify man page (like unSpawn wrote)
and to get a look at it, you can look at some example I did.

INOTIFY is the tool for that (in my opinion), you will not get any improper action if you use it on files.

good luck
 
Old 02-16-2012, 08:25 AM   #8
shdobxr
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lithos View Post
this really sounds like you should use inotify , inotify man page (like unSpawn wrote)
and to get a look at it, you can look at some example I did.

INOTIFY is the tool for that (in my opinion), you will not get any improper action if you use it on files.

good luck
Will do! Hey.....to everyone. Thanks for the assistance. Will update with my status as I don't believe google or LQ for that matter has something like this problem.
 
Old 02-16-2012, 09:06 AM   #9
shdobxr
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shdobxr View Post
Will do! Hey.....to everyone. Thanks for the assistance. Will update with my status as I don't believe google or LQ for that matter has something like this problem.
About an inch deep into the reading. Looks like this will fit the bill. Must I use incron? Or could one use a backrounder like a sample from LQ like this....


(sourced from this LQ link
Code:

#!/bin/bash

###
### Sample background worker script
### for linuxquestions.org written
### by Florian Harbich (user doc.nice)
###
### Free for use or modification, even if
### useless in this variant...
###

TERMINATORFILE="/var/run/backgroundworker.ctl"
GOON=1
while [ $GOON ]; do
[ -f "$TERMINATORFILE" ] && GOON=0
# do your repeated stuff instead of logger syslog sample here
logger -t BGWorker -- "hi! I'm happy to tell you i'm still alive"
sleep 1
done
rm -f "$TERMINATORFILE"
 
Old 03-01-2012, 08:13 AM   #10
shdobxr
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thought I would close this issue. The Inotify (incrontab) was exactly what I needed. Thank you LQ for the assist.

Dealing with complex file filtering and automation at work and this solves it. Please note that inotify deals with kernel interrupts and not valid for watching NFS mounts. There was a link here that gives more depth to why it doesn't work with NFS.

Thanks again LQ, back to stalking the forums.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto find files with ctime older than X seconds? spangberg Linux - General 2 06-05-2013 05:23 AM
Perl: Turning a seconds-since-the-epoch number into "X days ago" or similar AlucardZero Programming 2 08-26-2010 05:59 PM
Script to delete older files not running . linuxlover.chaitanya Linux - Newbie 10 01-12-2009 01:27 AM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Script for compressing files older than one year Pengu010011 Linux - Newbie 2 10-26-2004 07:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration