edujs7 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Trying to build up an small Perl application that uses running system processes just like the  top command does, in this case I need only specific fields like PID, Memory Size, CPU Time and Program Name. The idea of my program is to pull these processes and their info to then save them somewhere as a file to then use it to filter information by perhaps using  egrep > file.txt The main idea is for the program to be able to display the PID's info in different ways when running it - myprogram,pl (option) file.txt

My program must allow me to use options; different options will have to do different things, such as adding only CPU time for all running system tasks, adding memory size for all running system tasks, adding the CPU time for all current system processes, etc. I'm just not sure where to start at the moment - hope you can guys show me the light :) I'll keep this thread up to date with my progress (failed attempts I should say). Thanks so much in advance guys.

Replies are listed 'Best First'.
Re: Use running system processes and filter them on different ways
by Your Mother (Archbishop) on May 17, 2019 at 15:23 UTC
      Is this something I need to install or can be invoked? my OS is Ubuntu BTW I read about it but don't seem to be able to fully grasp the idea of it :(
        Is this something I need to install ... my OS is Ubuntu

        Yes, it is a module so it needs to be installed. If you are on bionic then libproc-processtable-perl is available for the system perl through apt.

        Sooner or later you are going to want to use a module which isn't available through apt. For that you can read A Guide to Installing Modules.

Re: Use running system processes and filter them on different ways
by daxim (Curate) on May 17, 2019 at 06:48 UTC
    Cut out the middle man. Read procfs directly.

      Would you please elaborate. To my understanding procsf is a file holding system processes information at any given time, am I right?

      How is procsf different from the  top command and how can I make use of it effectively so that I can filter information from it in different ways? Sorry for the dumb questions, just trying to understand it :)
        procsf is a file holding system processes information
        No, it's a file system (hence procfs) expressed as a whole hierarchy of directories and files.
        How is procsf different from the top command
        top and similar tools read files from procfs to aggregate and display process info. You don't need to read top output, you can read procfs yourself, which is more efficient and puts you fully in control. It's the same idea that you do not spawn ls, but use opendir/readdir/stat to get file info.
        how can I make use of it effectively
        man 5 proc

        These are simple files, you already know how to open files and read from them. It makes no difference that the files are virtual and do not reside on some permanent storage. That's one of the design goals in unix.

Re: Use running system processes and filter them on different ways
by haukex (Archbishop) on May 17, 2019 at 20:22 UTC