Skip to content
Returns an object containing process status in formatted arrays
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
README.md
package.json
process_status.js
test.js

README.md

process_status

I needed to display process information on a status page in a dashboard. From the command line I would type something like:

$ ps -ef | grep httpd
root      3199     1  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3252  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3253  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3254  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3255  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3256  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3257  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3258  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
apache    3259  3199  0 Sep17 ?        00:00:00 /usr/sbin/httpd
swright  10615  8615  0 14:51 pts/1    00:00:00 grep httpd

The process_status utility runs something like that with some formatting options and returns an object containing something like the following:

{ grep_str: 'httpd',
  user_format: [],
  pid: '3199',
  pgid: '3199',
  format_str: '%U%p%r%a',
  processes:
   [ 'root      3199  3199 /usr/sbin/httpd',
     'apache    3252  3199 /usr/sbin/httpd',
     'apache    3253  3199 /usr/sbin/httpd',
     'apache    3254  3199 /usr/sbin/httpd',
     'apache    3255  3199 /usr/sbin/httpd',
     'apache    3256  3199 /usr/sbin/httpd',
     'apache    3257  3199 /usr/sbin/httpd',
     'apache    3258  3199 /usr/sbin/httpd',
     'apache    3259  3199 /usr/sbin/httpd' ],
  format_array: [ 'user', 'pid', 'pgid', 'args' ],
  formatted_process_list:
   [ [ 'root', '3199', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3252', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3253', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3254', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3255', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3256', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3257', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3258', '3199', '/usr/sbin/httpd' ],
     [ 'apache', '3259', '3199', '/usr/sbin/httpd' ] ] }

You can then use the display the data using the processes array or for prettier output you can use the formatted_process_list array to generate a table and the format_array to generate table headers.

Usage:

/**
*  Finds a process based on the grep string
*  
*  @param  grep_str    a string that will be used to grep the process list
*  @param   user_format     an array of friendly strings that will be used to 
*                           get string formatters from the formatters array
*  @param   cb              callback  
*/
get_status(grep_str, user_format, cb)

To generate the formatted_process_list array

/**
* @param   status      A status object created by get_status
* @param   cb          A callback function
*/
process_status_data(status, cb)

See the test.js file for an implementation example.

Something went wrong with that request. Please try again.