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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-24-2005, 09:35 AM
|
#1
|
|
Member
Registered: Aug 2004
Location: Western Australia
Distribution: Debian Lenny
Posts: 779
Rep:
|
cron job to detect new files
I need to have a cron job detecting the insertion of new files in a directory and its subdirectories and trigger a PHP script if there is a new file or new files.
The idea is to do some maintenance if there are new file(s) and chmod these files from 644 to 444. The cron job would run every ten seconds or so.
My knowledge of PHP and BASH is very basic and I have no idea how to do this, could anyone make a suggestion? I hope it's simple enough.
Thank you for your help.
|
|
|
|
12-26-2005, 11:31 AM
|
#2
|
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 42,711
|
cron doesn't run more often than once a minute, and that's really not what cron is for. it'd also have to be a pretty crude solution that would need to run from a cron script. There are tools like fam (might have been renamed gam recently... not sure) which will watch a filesystem intelligently and notify you of changes as you tell it to. you should have a look at things like this to help you out.
|
|
|
|
12-26-2005, 06:45 PM
|
#3
|
|
Member
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116
Rep:
|
sleep command in bash
You could have the cron call a script that has a loop that uses the sleep command.
|
|
|
|
12-26-2005, 09:30 PM
|
#4
|
|
Member
Registered: Aug 2004
Location: Western Australia
Distribution: Debian Lenny
Posts: 779
Original Poster
Rep:
|
Thank you for your answers. I saw a little bit of info about FAM, it seems to have some problems concerning recursive directories and subdirectories and the "sleep" mode (in cron job) keeps the processor busy.
Would you have any comment?
|
|
|
|
12-26-2005, 11:35 PM
|
#5
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
I tend to agree with acid_kewpie both that "cron" is probably not a good choice, and that you probably want to investigate other tools that are out there, for example, "inotify":
http://www-128.ibm.com/developerwork...l-inotify.html
For the sake of argument, however, could you get by with a simple script that does something like this (pseudo-code only):
Code:
N=60
while true; do
for dir in `cat SOME_DIRS.LST`; do
ls -lt | awk '{ SOME PROCESSING ON THE TIMESTAMP-SORTED OUTPUT FOR THIS DIRECTORY }' > CURRENT FILES.LST
if (diff CURRENT_FILES.LST PREVIOUS_FILES.LST); then
PROCESS NEW FILES
fi
done
mv CURRENT_FILES.LIST PREVIOUS_FILES.LST
sleep N
done
|
|
|
|
12-28-2005, 05:26 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
Quote:
|
Originally Posted by dugas
You could have the cron call a script that has a loop that uses the sleep command.
|
Ugggh! That's awful.
Off to bed with no tea. 
|
|
|
|
12-28-2005, 05:44 AM
|
#7
|
|
Member
Registered: Apr 2003
Location: Waterford, Ireland
Distribution: Mandrivia 2006.1, Ubuntu 7.10
Posts: 188
Rep:
|
I get that sort of request sometimes in Projects myself. Thanks
|
|
|
|
12-28-2005, 06:03 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
why not
touch a timestamp file each time and use find -newer
Code:
find . -newer stamp
touch stamp
or even better, touch -t to give an explicit timestamp.
Code:
touch -t DDMMYYYHHMM stamp
find . -newer stamp
|
|
|
|
12-28-2005, 08:51 AM
|
#9
|
|
Member
Registered: Aug 2004
Location: Western Australia
Distribution: Debian Lenny
Posts: 779
Original Poster
Rep:
|
Thank you all, I've got plenty to try now.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:43 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|