Find the answer to your Linux question:
Results 1 to 4 of 4
Hello everyone, first time poster and I have a question for you. I'm trying to run a php script whenever a specific directory's files and/or folders are edited/added/deleted. It has ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    2

    Help: Execute php script on directory content change.


    Hello everyone, first time poster and I have a question for you.

    I'm trying to run a php script whenever a specific directory's files and/or folders are edited/added/deleted.

    It has to be run through CLI.

    Any suggestions on how to do this?

    Thanks!

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    The simplest way would be to use cron.
    linux user # 503963

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    2
    Quote Originally Posted by scathefire View Post
    The simplest way would be to use cron.
    Thanks, but I cant use cron since the directory gets changed randomly. And I want the php to run everytime the directory changes. I want it to run within the same minute as possible.

    Unless theres a way to cron it that I dont know yet.

  4. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,382
    Hi,
    Inotify is what you want.
    There are several userland tools.

    1) incrond
    This is a relatively simple tool. Similar to cron but fs event based.
    It can watch multiple directories, but not recursive.
    Once one or multiple events such as IN_CLOSE_WRITE is/are received, it executes a given script/tool.

    Note: incrond is very picky about tabs/white spaces/line feeds.
    - Only use *one* space to separate the parts of a incrontab.
    - Never use tabs (On that one I agree, actually )
    - There needs to be a line feed at the end of the file

    2) lsyncd
    Contrary to what the name suggests, its capabilities exceed file syncing.
    By far.
    It can watch directories recursively.
    The actions are controlled via lua, aka it is very versatile.

    3) PHP Pecl Inotify
    This would have the advantage to not mix multiple languages.
    But then again -in your case- you would need to run your php application as a daemon
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •