Execute command when a file changes












9














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.










share|improve this question
























  • Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
    – O.O.
    yesterday










  • FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
    – Sergiy Kolodyazhnyy
    16 hours ago
















9














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.










share|improve this question
























  • Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
    – O.O.
    yesterday










  • FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
    – Sergiy Kolodyazhnyy
    16 hours ago














9












9








9


2





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.










share|improve this question















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.







linux unix cron automation inotify






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Peter Mortensen

2,09742124




2,09742124










asked yesterday









AlexAlex

462




462












  • Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
    – O.O.
    yesterday










  • FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
    – Sergiy Kolodyazhnyy
    16 hours ago


















  • Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
    – O.O.
    yesterday










  • FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
    – Sergiy Kolodyazhnyy
    16 hours ago
















Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
– O.O.
yesterday




Have you looked at eradman.com/entrproject , haven't tried it myself but it looks like it may be related.
– O.O.
yesterday












FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
– Sergiy Kolodyazhnyy
16 hours ago




FYI, Python has inotify libraries available. See one of my answers here for an example: askubuntu.com/a/939392/295286
– Sergiy Kolodyazhnyy
16 hours ago










4 Answers
4






active

oldest

votes


















10














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 





share|improve this answer



















  • 2




    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.
    – Gerald Schneider
    yesterday










  • Thanks for reminding me, I have added the context for the link.
    – victoroloan
    yesterday












  • 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 ?
    – Alex
    yesterday






  • 1




    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
    – victoroloan
    yesterday












  • 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
    – Ferrybig
    yesterday



















1














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|improve this answer








New contributor




jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    0














    The watchexec (https://crates.io/crates/watchexec) command line utility sounds like exactly what you need, although I believe to install it you'd need to have the Rust build tools installed on your machine, so that may be a dealbreaker






    share|improve this answer








    New contributor




    Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
      – Nathaniel Pisarski
      16 hours ago



















    0














    My general approach would be to fiddle with the classical Unix find utility. For example, the command



    find /tmp/upload_data/*.csv -mtime -1 -exec /home/myname/test.py


    will find any .csv files in /tmp/upload_data that have been modified less than one day ago, and run your test.py if it finds any. Of course, if your test.py file is in some other directory, you want to update your path to it accordingly.



    If you run your cron job more often than once a day, you can use the mmin option to find to specify the maximal time since modification in minutes. For example,



    find /tmp/upload_data/*.csv -mmin -60 -exec /home/myname/test.py


    will search for .csv files that were modified less than 60 minutes ago -- useful if cron runs the job hourly.



    Two fair warnings are in order: First, this won't catch .csv files that you entirely deleted. You may want to check for these separately. Second, I did not have time to test any of this. Expect typos in my code that you'll have to debug by yourself.






    share|improve this answer










    New contributor




    Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.














    • 1




      What is the -cmd syntax? IIRC find takes -exec cmd ;...
      – D. Ben Knoble
      14 hours ago










    • I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
      – Alex
      11 hours ago










    • @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
      – Thomas Blankenhorn
      10 hours ago













    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "2"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f947868%2fexecute-command-when-a-file-changes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    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 





    share|improve this answer



















    • 2




      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.
      – Gerald Schneider
      yesterday










    • Thanks for reminding me, I have added the context for the link.
      – victoroloan
      yesterday












    • 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 ?
      – Alex
      yesterday






    • 1




      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
      – victoroloan
      yesterday












    • 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
      – Ferrybig
      yesterday
















    10














    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 





    share|improve this answer



















    • 2




      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.
      – Gerald Schneider
      yesterday










    • Thanks for reminding me, I have added the context for the link.
      – victoroloan
      yesterday












    • 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 ?
      – Alex
      yesterday






    • 1




      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
      – victoroloan
      yesterday












    • 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
      – Ferrybig
      yesterday














    10












    10








    10






    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 





    share|improve this answer














    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 






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 10 hours ago









    Jenny D

    23.3k116093




    23.3k116093










    answered yesterday









    victoroloanvictoroloan

    1514




    1514








    • 2




      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.
      – Gerald Schneider
      yesterday










    • Thanks for reminding me, I have added the context for the link.
      – victoroloan
      yesterday












    • 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 ?
      – Alex
      yesterday






    • 1




      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
      – victoroloan
      yesterday












    • 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
      – Ferrybig
      yesterday














    • 2




      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.
      – Gerald Schneider
      yesterday










    • Thanks for reminding me, I have added the context for the link.
      – victoroloan
      yesterday












    • 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 ?
      – Alex
      yesterday






    • 1




      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
      – victoroloan
      yesterday












    • 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
      – Ferrybig
      yesterday








    2




    2




    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.
    – Gerald Schneider
    yesterday




    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.
    – Gerald Schneider
    yesterday












    Thanks for reminding me, I have added the context for the link.
    – victoroloan
    yesterday






    Thanks for reminding me, I have added the context for the link.
    – victoroloan
    yesterday














    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 ?
    – Alex
    yesterday




    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 ?
    – Alex
    yesterday




    1




    1




    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
    – victoroloan
    yesterday






    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
    – victoroloan
    yesterday














    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
    – Ferrybig
    yesterday




    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
    – Ferrybig
    yesterday













    1














    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|improve this answer








    New contributor




    jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1














      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|improve this answer








      New contributor




      jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        1












        1








        1






        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|improve this answer








        New contributor




        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        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|improve this answer








        New contributor




        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered yesterday









        jln-hojln-ho

        111




        111




        New contributor




        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        jln-ho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.























            0














            The watchexec (https://crates.io/crates/watchexec) command line utility sounds like exactly what you need, although I believe to install it you'd need to have the Rust build tools installed on your machine, so that may be a dealbreaker






            share|improve this answer








            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
              – Nathaniel Pisarski
              16 hours ago
















            0














            The watchexec (https://crates.io/crates/watchexec) command line utility sounds like exactly what you need, although I believe to install it you'd need to have the Rust build tools installed on your machine, so that may be a dealbreaker






            share|improve this answer








            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
              – Nathaniel Pisarski
              16 hours ago














            0












            0








            0






            The watchexec (https://crates.io/crates/watchexec) command line utility sounds like exactly what you need, although I believe to install it you'd need to have the Rust build tools installed on your machine, so that may be a dealbreaker






            share|improve this answer








            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            The watchexec (https://crates.io/crates/watchexec) command line utility sounds like exactly what you need, although I believe to install it you'd need to have the Rust build tools installed on your machine, so that may be a dealbreaker







            share|improve this answer








            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered yesterday









            Ben SandeenBen Sandeen

            1011




            1011




            New contributor




            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Ben Sandeen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
              – Nathaniel Pisarski
              16 hours ago


















            • I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
              – Nathaniel Pisarski
              16 hours ago
















            I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
            – Nathaniel Pisarski
            16 hours ago




            I love using software written in rust because you know it wasn't abandoned in 2004 or something. It almost has to be new.
            – Nathaniel Pisarski
            16 hours ago











            0














            My general approach would be to fiddle with the classical Unix find utility. For example, the command



            find /tmp/upload_data/*.csv -mtime -1 -exec /home/myname/test.py


            will find any .csv files in /tmp/upload_data that have been modified less than one day ago, and run your test.py if it finds any. Of course, if your test.py file is in some other directory, you want to update your path to it accordingly.



            If you run your cron job more often than once a day, you can use the mmin option to find to specify the maximal time since modification in minutes. For example,



            find /tmp/upload_data/*.csv -mmin -60 -exec /home/myname/test.py


            will search for .csv files that were modified less than 60 minutes ago -- useful if cron runs the job hourly.



            Two fair warnings are in order: First, this won't catch .csv files that you entirely deleted. You may want to check for these separately. Second, I did not have time to test any of this. Expect typos in my code that you'll have to debug by yourself.






            share|improve this answer










            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 1




              What is the -cmd syntax? IIRC find takes -exec cmd ;...
              – D. Ben Knoble
              14 hours ago










            • I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
              – Alex
              11 hours ago










            • @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
              – Thomas Blankenhorn
              10 hours ago


















            0














            My general approach would be to fiddle with the classical Unix find utility. For example, the command



            find /tmp/upload_data/*.csv -mtime -1 -exec /home/myname/test.py


            will find any .csv files in /tmp/upload_data that have been modified less than one day ago, and run your test.py if it finds any. Of course, if your test.py file is in some other directory, you want to update your path to it accordingly.



            If you run your cron job more often than once a day, you can use the mmin option to find to specify the maximal time since modification in minutes. For example,



            find /tmp/upload_data/*.csv -mmin -60 -exec /home/myname/test.py


            will search for .csv files that were modified less than 60 minutes ago -- useful if cron runs the job hourly.



            Two fair warnings are in order: First, this won't catch .csv files that you entirely deleted. You may want to check for these separately. Second, I did not have time to test any of this. Expect typos in my code that you'll have to debug by yourself.






            share|improve this answer










            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.














            • 1




              What is the -cmd syntax? IIRC find takes -exec cmd ;...
              – D. Ben Knoble
              14 hours ago










            • I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
              – Alex
              11 hours ago










            • @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
              – Thomas Blankenhorn
              10 hours ago
















            0












            0








            0






            My general approach would be to fiddle with the classical Unix find utility. For example, the command



            find /tmp/upload_data/*.csv -mtime -1 -exec /home/myname/test.py


            will find any .csv files in /tmp/upload_data that have been modified less than one day ago, and run your test.py if it finds any. Of course, if your test.py file is in some other directory, you want to update your path to it accordingly.



            If you run your cron job more often than once a day, you can use the mmin option to find to specify the maximal time since modification in minutes. For example,



            find /tmp/upload_data/*.csv -mmin -60 -exec /home/myname/test.py


            will search for .csv files that were modified less than 60 minutes ago -- useful if cron runs the job hourly.



            Two fair warnings are in order: First, this won't catch .csv files that you entirely deleted. You may want to check for these separately. Second, I did not have time to test any of this. Expect typos in my code that you'll have to debug by yourself.






            share|improve this answer










            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            My general approach would be to fiddle with the classical Unix find utility. For example, the command



            find /tmp/upload_data/*.csv -mtime -1 -exec /home/myname/test.py


            will find any .csv files in /tmp/upload_data that have been modified less than one day ago, and run your test.py if it finds any. Of course, if your test.py file is in some other directory, you want to update your path to it accordingly.



            If you run your cron job more often than once a day, you can use the mmin option to find to specify the maximal time since modification in minutes. For example,



            find /tmp/upload_data/*.csv -mmin -60 -exec /home/myname/test.py


            will search for .csv files that were modified less than 60 minutes ago -- useful if cron runs the job hourly.



            Two fair warnings are in order: First, this won't catch .csv files that you entirely deleted. You may want to check for these separately. Second, I did not have time to test any of this. Expect typos in my code that you'll have to debug by yourself.







            share|improve this answer










            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited 10 hours ago





















            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 17 hours ago









            Thomas BlankenhornThomas Blankenhorn

            1012




            1012




            New contributor




            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Thomas Blankenhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.








            • 1




              What is the -cmd syntax? IIRC find takes -exec cmd ;...
              – D. Ben Knoble
              14 hours ago










            • I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
              – Alex
              11 hours ago










            • @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
              – Thomas Blankenhorn
              10 hours ago
















            • 1




              What is the -cmd syntax? IIRC find takes -exec cmd ;...
              – D. Ben Knoble
              14 hours ago










            • I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
              – Alex
              11 hours ago










            • @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
              – Thomas Blankenhorn
              10 hours ago










            1




            1




            What is the -cmd syntax? IIRC find takes -exec cmd ;...
            – D. Ben Knoble
            14 hours ago




            What is the -cmd syntax? IIRC find takes -exec cmd ;...
            – D. Ben Knoble
            14 hours ago












            I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
            – Alex
            11 hours ago




            I have tried this one before posting this question ,this is not working properly on 2nd 3rd consecutive run of cron jobs
            – Alex
            11 hours ago












            @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
            – Thomas Blankenhorn
            10 hours ago






            @D. Ben Knoble: You're right. I mixed up find-internal commands with shell commands. Fixed. Thanks for the correction!
            – Thomas Blankenhorn
            10 hours ago




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Server Fault!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f947868%2fexecute-command-when-a-file-changes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            An IMO inspired problem

            Management

            Has there ever been an instance of an active nuclear power plant within or near a war zone?