LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-24-2005, 09:35 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Rep: Reputation: 52
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.
 
Old 12-26-2005, 11:31 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 12-26-2005, 06:45 PM   #3
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
sleep command in bash

You could have the cron call a script that has a loop that uses the sleep command.
 
Old 12-26-2005, 09:30 PM   #4
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
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?
 
Old 12-26-2005, 11:35 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 12-28-2005, 05:26 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 12-28-2005, 05:44 AM   #7
hamster
Member
 
Registered: Apr 2003
Location: Waterford, Ireland
Distribution: Mandrivia 2006.1, Ubuntu 7.10
Posts: 188

Rep: Reputation: 30
I get that sort of request sometimes in Projects myself. Thanks
 
Old 12-28-2005, 06:03 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 12-28-2005, 08:51 AM   #9
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
Thank you all, I've got plenty to try now.
 
  


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
Copy files with cron greg334 Linux - Newbie 1 02-21-2005 10:13 PM
Cron - Delete Files nigeledge Linux - Software 1 10-14-2004 10:36 PM
Cron is not outputting log files... Clubber Lang Linux - Newbie 2 09-27-2004 04:41 AM
howto delete files via ftp from cron job ? cccc Linux - Networking 2 01-31-2004 06:49 PM
cron and tar files blown99GT Linux - Newbie 1 06-25-2002 05:10 AM

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

All times are GMT -5. The time now is 10:36 PM.

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