How to automate the process of executing a command upon creation of a file












2















How to automate the process of executing a command upon creation of a file.
I have an application wherein a tool generates an xsd schema from a xml file. My next module uses this xsd schema for further processing.

I want to automate this process i.e. to run a command once the file is generated. This application will run everyday on ubuntu machine so I want to automate it. I don't want any human intervention in this process.
I did search inotify lib which is used for similar purposes but it forces me to change the enitre design of my system.

Are there any other alternatives which would solve this purpose? Any help is highly appreciated.










share|improve this question





























    2















    How to automate the process of executing a command upon creation of a file.
    I have an application wherein a tool generates an xsd schema from a xml file. My next module uses this xsd schema for further processing.

    I want to automate this process i.e. to run a command once the file is generated. This application will run everyday on ubuntu machine so I want to automate it. I don't want any human intervention in this process.
    I did search inotify lib which is used for similar purposes but it forces me to change the enitre design of my system.

    Are there any other alternatives which would solve this purpose? Any help is highly appreciated.










    share|improve this question



























      2












      2








      2








      How to automate the process of executing a command upon creation of a file.
      I have an application wherein a tool generates an xsd schema from a xml file. My next module uses this xsd schema for further processing.

      I want to automate this process i.e. to run a command once the file is generated. This application will run everyday on ubuntu machine so I want to automate it. I don't want any human intervention in this process.
      I did search inotify lib which is used for similar purposes but it forces me to change the enitre design of my system.

      Are there any other alternatives which would solve this purpose? Any help is highly appreciated.










      share|improve this question
















      How to automate the process of executing a command upon creation of a file.
      I have an application wherein a tool generates an xsd schema from a xml file. My next module uses this xsd schema for further processing.

      I want to automate this process i.e. to run a command once the file is generated. This application will run everyday on ubuntu machine so I want to automate it. I don't want any human intervention in this process.
      I did search inotify lib which is used for similar purposes but it forces me to change the enitre design of my system.

      Are there any other alternatives which would solve this purpose? Any help is highly appreciated.







      files






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 30 '14 at 6:16









      girardengo

      3,8021627




      3,8021627










      asked Apr 30 '14 at 6:10









      user3168180user3168180

      112




      112






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Figuring out when the file is finished being written is the hard part. It is best to use a script like:



          #!/bin/sh
          program-that-creates-xsd
          program-that-does-further-processing


          There really aren't any good solutions if you can't structure your program like this. Even with inotify, it is nearly impossible to tell when the XSD is truly finished.






          share|improve this answer































            0














            Detecting file creation in a specific folder can be done via inotifywait which is part of inotify-tools package installable via sudo apt-get install inotify-tools.



            As an example, we can establish a continuous watch (-m flag) on directory Desktop and parse the output for created directory entries, however determining if a file is regular file and filename is the one we need has to be done manually:



            inotifywait -m --format "%f" -e create ./Desktop/  | 
            while read -r fname; do
            [ -f ./Desktop/"$fname" ] && [ "$fname" = "specific.csv" ] && echo "We got 'em!"
            done


            So in case someone makes directory specific.csv that should be rejected, but if specific.csv is created we will execute echo portion, which could be replaced with other commands or shell functions. If you do need a non-blocking execution, you might consider adding setsid to start the command in new session and let the script continue watching.






            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              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%2faskubuntu.com%2fquestions%2f458102%2fhow-to-automate-the-process-of-executing-a-command-upon-creation-of-a-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Figuring out when the file is finished being written is the hard part. It is best to use a script like:



              #!/bin/sh
              program-that-creates-xsd
              program-that-does-further-processing


              There really aren't any good solutions if you can't structure your program like this. Even with inotify, it is nearly impossible to tell when the XSD is truly finished.






              share|improve this answer




























                0














                Figuring out when the file is finished being written is the hard part. It is best to use a script like:



                #!/bin/sh
                program-that-creates-xsd
                program-that-does-further-processing


                There really aren't any good solutions if you can't structure your program like this. Even with inotify, it is nearly impossible to tell when the XSD is truly finished.






                share|improve this answer


























                  0












                  0








                  0







                  Figuring out when the file is finished being written is the hard part. It is best to use a script like:



                  #!/bin/sh
                  program-that-creates-xsd
                  program-that-does-further-processing


                  There really aren't any good solutions if you can't structure your program like this. Even with inotify, it is nearly impossible to tell when the XSD is truly finished.






                  share|improve this answer













                  Figuring out when the file is finished being written is the hard part. It is best to use a script like:



                  #!/bin/sh
                  program-that-creates-xsd
                  program-that-does-further-processing


                  There really aren't any good solutions if you can't structure your program like this. Even with inotify, it is nearly impossible to tell when the XSD is truly finished.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 30 '14 at 6:24









                  mswmsw

                  4,19611826




                  4,19611826

























                      0














                      Detecting file creation in a specific folder can be done via inotifywait which is part of inotify-tools package installable via sudo apt-get install inotify-tools.



                      As an example, we can establish a continuous watch (-m flag) on directory Desktop and parse the output for created directory entries, however determining if a file is regular file and filename is the one we need has to be done manually:



                      inotifywait -m --format "%f" -e create ./Desktop/  | 
                      while read -r fname; do
                      [ -f ./Desktop/"$fname" ] && [ "$fname" = "specific.csv" ] && echo "We got 'em!"
                      done


                      So in case someone makes directory specific.csv that should be rejected, but if specific.csv is created we will execute echo portion, which could be replaced with other commands or shell functions. If you do need a non-blocking execution, you might consider adding setsid to start the command in new session and let the script continue watching.






                      share|improve this answer




























                        0














                        Detecting file creation in a specific folder can be done via inotifywait which is part of inotify-tools package installable via sudo apt-get install inotify-tools.



                        As an example, we can establish a continuous watch (-m flag) on directory Desktop and parse the output for created directory entries, however determining if a file is regular file and filename is the one we need has to be done manually:



                        inotifywait -m --format "%f" -e create ./Desktop/  | 
                        while read -r fname; do
                        [ -f ./Desktop/"$fname" ] && [ "$fname" = "specific.csv" ] && echo "We got 'em!"
                        done


                        So in case someone makes directory specific.csv that should be rejected, but if specific.csv is created we will execute echo portion, which could be replaced with other commands or shell functions. If you do need a non-blocking execution, you might consider adding setsid to start the command in new session and let the script continue watching.






                        share|improve this answer


























                          0












                          0








                          0







                          Detecting file creation in a specific folder can be done via inotifywait which is part of inotify-tools package installable via sudo apt-get install inotify-tools.



                          As an example, we can establish a continuous watch (-m flag) on directory Desktop and parse the output for created directory entries, however determining if a file is regular file and filename is the one we need has to be done manually:



                          inotifywait -m --format "%f" -e create ./Desktop/  | 
                          while read -r fname; do
                          [ -f ./Desktop/"$fname" ] && [ "$fname" = "specific.csv" ] && echo "We got 'em!"
                          done


                          So in case someone makes directory specific.csv that should be rejected, but if specific.csv is created we will execute echo portion, which could be replaced with other commands or shell functions. If you do need a non-blocking execution, you might consider adding setsid to start the command in new session and let the script continue watching.






                          share|improve this answer













                          Detecting file creation in a specific folder can be done via inotifywait which is part of inotify-tools package installable via sudo apt-get install inotify-tools.



                          As an example, we can establish a continuous watch (-m flag) on directory Desktop and parse the output for created directory entries, however determining if a file is regular file and filename is the one we need has to be done manually:



                          inotifywait -m --format "%f" -e create ./Desktop/  | 
                          while read -r fname; do
                          [ -f ./Desktop/"$fname" ] && [ "$fname" = "specific.csv" ] && echo "We got 'em!"
                          done


                          So in case someone makes directory specific.csv that should be rejected, but if specific.csv is created we will execute echo portion, which could be replaced with other commands or shell functions. If you do need a non-blocking execution, you might consider adding setsid to start the command in new session and let the script continue watching.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          Sergiy KolodyazhnyySergiy Kolodyazhnyy

                          72.8k9152316




                          72.8k9152316






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • 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%2faskubuntu.com%2fquestions%2f458102%2fhow-to-automate-the-process-of-executing-a-command-upon-creation-of-a-file%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

                              GameSpot

                              connect to host localhost port 22: Connection refused

                              Getting a Wifi WPA2 wifi connection