running script every 1 second... mv'ing files once ctime = 5 seconds older than epoch
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#!/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
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.
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
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.
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
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.
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.
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....
###
### 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"
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.