Poor man’s processes monitor (bash & awk script)

Recently a member of the Romanian Ubuntu Community asked for a script to monitor the running processes on his server . He didn’t requested for anything fancy, just a small utility that will be able to detect a hanging application (a process that is “eating” more than 80% of the CPU for a long period of time) and then log the results .

I am no sysadmin, but I am sure there are a lot of dedicated open-source solutions for monitoring a server .Still the functionality he asked for can be easily achieved by combining bash and awk .

One of the things I like Linux for is the power of the shell and the full-control over the your system . You can write a script for every repeating task that arise as bash is easy to learn but of course, hard to master .  More as an exercise for myself I’ve proposed the following solution :

If you run this script the output will probably look similar to this one :

The output can then be redirected to a file (>>) and interpreted as: Read More

How to get the active process list on a Linux machine using python

To retrieve the active process list on a Linux machine we will use the subprocess module . This module will allow us to create new subprocesses, connect to their input / output / error pipes, and eventually retrieve their exit codes .

It offers us a higher level and cleaner approach by defining only one class: subprocess.Popen(), intended to replace the functionality offered by methods such as: os.system(), os.popen() or os.popen2() .

To obtain the active processes and their associated information (USER, PID, CPU, MEM, TTY, STAT, TIME, COMMAND, etc.) we will spawn a ps aux subprocess, connect to its output pipe and parse the results .

But first let’s analyze a little the output of ps aux :

Our first step will be to create a data structure capable of encapsulating the above information . We will write the Proc class that will wrap all the process meta-information present in the ‘ps aux‘ output :

A list of Proc objects will probably do the job . Read More