Execute command when a file changes

9,472

Solution 1

You might need incrond (inotify cron daemon) which will monitors changes on files and then execute scripts.

Incrond can monitor add new file, modify, delete and many more. This is an article shows what event incrond can monitor with some example.

Example for your case, you might create the file /etc/incron.d/data_upload with the contents

/tmp/data_upload IN_CREATE,IN_MODIFY /path/to/test.py 

Solution 2

You could use entr to automatically run the script everytime a file changes by running ls /tmp/data_upload | entr -p script.py once at startup.

Project website: http://eradman.com/entrproject/

Online man page: https://www.systutorials.com/docs/linux/man/1-entr/

Share:
9,472

Related videos on Youtube

Alex
Author by

Alex

Updated on September 18, 2022

Comments

  • Alex
    Alex over 1 year

    I have a scenario where I am uploading .csv files to a specific folder, /tmp/data_upload, every day, and the old files are replaced by the new one.

    I need to run a Python script once the data is uploaded. For this, I have an idea to create a cron job and monitor the changes in the file. I tried using inotify, but I am not much into the Unix domain. How can I do that?

    I need to execute the script test.py once there is a date change of a file in the upload folder, for example, /tmp/data_upload.

    • O.O.
      O.O. over 5 years
      Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 5 years
      FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
  • dortegaoh
    dortegaoh over 5 years
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • victoroloan
    victoroloan over 5 years
    Thanks for reminding me, I have added the context for the link.
  • Alex
    Alex over 5 years
    Thanks for the answer, just to verify the steps after installing incrontab shoudl execute incrontab -e as root then include this line /tmp/data_upload IN_CREATE,IN_MODIFY test.py ? so that to check once I upload a new file it should execute the test.py file ? where should I place the test.py file ? should i need to provide absolute path for this ?
  • victoroloan
    victoroloan over 5 years
    I think, It will be better to put the absolute path for your script. You can also check cron or system log if the script seems not working
  • Ferrybig
    Ferrybig over 5 years
    Can you also document what file you are referring to with your code block, people who are not familiar with the syntax of Incrond (like me) may think are referring to a command that you have to execute on the command line
  • Nathaniel Pisarski
    Nathaniel Pisarski over 5 years
    I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
  • Alex
    Alex over 5 years
    I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
  • Thomas Blankenhorn
    Thomas Blankenhorn over 5 years
    @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
  • Jenny D
    Jenny D over 5 years
    @Ferrybig I think I added enough information to cover that now