How to record PID and its command every 15s?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












For troubleshooting purpose, I'd like to record PID and its command every 15s and write them to a text file.



The desired output:
TimeStamp, PID, Command










share|improve this question

























    up vote
    -1
    down vote

    favorite












    For troubleshooting purpose, I'd like to record PID and its command every 15s and write them to a text file.



    The desired output:
    TimeStamp, PID, Command










    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      For troubleshooting purpose, I'd like to record PID and its command every 15s and write them to a text file.



      The desired output:
      TimeStamp, PID, Command










      share|improve this question













      For troubleshooting purpose, I'd like to record PID and its command every 15s and write them to a text file.



      The desired output:
      TimeStamp, PID, Command







      linux process ps top htop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      John Hass

      1524




      1524




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          This will get you you the three columns you want:



          $ ps -hopid,comm | perl -anle'print time, ", $F[0], $F[1]"'
          1541626566, 6496, gedit
          1541626566, 7513, bash


          You can limit or extend the scope of ps, ie. what processes it lists. Then you put this in a loop with redirection.



          while true; do echo x; sleep 15; done > out


          Replace echo x with the command proper and out with the file name you choose.



          As for the command producing the information, here's a run-down.




          • ps -hopid,comm - ps is obvious, -h turns off the header line and -o stands for the output (PID and command).

          • This goes to the Perl command, namely perl -anle'print time, ", $F[0], $F[1]"'. Here perl is Perl. The -anle flags reprent correspondingly: a - load input into an array, n - take care of new lines, l - process each single line, and finally e - execute the code that follows.

          • And now the code follows: 'print time, ", $F[0], $F[1]"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f480456%2fhow-to-record-pid-and-its-command-every-15s%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            This will get you you the three columns you want:



            $ ps -hopid,comm | perl -anle'print time, ", $F[0], $F[1]"'
            1541626566, 6496, gedit
            1541626566, 7513, bash


            You can limit or extend the scope of ps, ie. what processes it lists. Then you put this in a loop with redirection.



            while true; do echo x; sleep 15; done > out


            Replace echo x with the command proper and out with the file name you choose.



            As for the command producing the information, here's a run-down.




            • ps -hopid,comm - ps is obvious, -h turns off the header line and -o stands for the output (PID and command).

            • This goes to the Perl command, namely perl -anle'print time, ", $F[0], $F[1]"'. Here perl is Perl. The -anle flags reprent correspondingly: a - load input into an array, n - take care of new lines, l - process each single line, and finally e - execute the code that follows.

            • And now the code follows: 'print time, ", $F[0], $F[1]"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.





            share|improve this answer


























              up vote
              0
              down vote













              This will get you you the three columns you want:



              $ ps -hopid,comm | perl -anle'print time, ", $F[0], $F[1]"'
              1541626566, 6496, gedit
              1541626566, 7513, bash


              You can limit or extend the scope of ps, ie. what processes it lists. Then you put this in a loop with redirection.



              while true; do echo x; sleep 15; done > out


              Replace echo x with the command proper and out with the file name you choose.



              As for the command producing the information, here's a run-down.




              • ps -hopid,comm - ps is obvious, -h turns off the header line and -o stands for the output (PID and command).

              • This goes to the Perl command, namely perl -anle'print time, ", $F[0], $F[1]"'. Here perl is Perl. The -anle flags reprent correspondingly: a - load input into an array, n - take care of new lines, l - process each single line, and finally e - execute the code that follows.

              • And now the code follows: 'print time, ", $F[0], $F[1]"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.





              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                This will get you you the three columns you want:



                $ ps -hopid,comm | perl -anle'print time, ", $F[0], $F[1]"'
                1541626566, 6496, gedit
                1541626566, 7513, bash


                You can limit or extend the scope of ps, ie. what processes it lists. Then you put this in a loop with redirection.



                while true; do echo x; sleep 15; done > out


                Replace echo x with the command proper and out with the file name you choose.



                As for the command producing the information, here's a run-down.




                • ps -hopid,comm - ps is obvious, -h turns off the header line and -o stands for the output (PID and command).

                • This goes to the Perl command, namely perl -anle'print time, ", $F[0], $F[1]"'. Here perl is Perl. The -anle flags reprent correspondingly: a - load input into an array, n - take care of new lines, l - process each single line, and finally e - execute the code that follows.

                • And now the code follows: 'print time, ", $F[0], $F[1]"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.





                share|improve this answer














                This will get you you the three columns you want:



                $ ps -hopid,comm | perl -anle'print time, ", $F[0], $F[1]"'
                1541626566, 6496, gedit
                1541626566, 7513, bash


                You can limit or extend the scope of ps, ie. what processes it lists. Then you put this in a loop with redirection.



                while true; do echo x; sleep 15; done > out


                Replace echo x with the command proper and out with the file name you choose.



                As for the command producing the information, here's a run-down.




                • ps -hopid,comm - ps is obvious, -h turns off the header line and -o stands for the output (PID and command).

                • This goes to the Perl command, namely perl -anle'print time, ", $F[0], $F[1]"'. Here perl is Perl. The -anle flags reprent correspondingly: a - load input into an array, n - take care of new lines, l - process each single line, and finally e - execute the code that follows.

                • And now the code follows: 'print time, ", $F[0], $F[1]"'. First print the time stamp, then the first column of the array that holds the input material, and then the second.






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 43 mins ago

























                answered 52 mins ago









                Tomasz

                8,62052761




                8,62052761



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480456%2fhow-to-record-pid-and-its-command-every-15s%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?