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.
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.
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.
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.
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":
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.