3

I have this code in bash and do not know how to translate the lines with 'ps -C.....' and ' ps -eo....' any help in how to write these lines?

if [ "$SwapUsedActivity" == "true" ]
then 
    echo "WARNING: Potential Swapping happening please check for increasing swap usage"
    echo "INFO: checking kswapd number of process and cputime, check for TIME colume for how busy kswapd was"
    ps -C kswapd0 -C kswapd1 -C kswapd2 -C kswapd3 -C kswapd4  -o pid,ppid,stime,etime,time,pcpu,args
else
    echo "INFO: High swap space usage detected, not a potential issue but check activity"
fi
else
    echo "INFO: Swap usage within the limits"
fi
if [ $AvgAnnonymousMemoryused -gt 20 ]
then 
    echo "INFO: High residential memory usage detected, this may not be an issue, please check top memory consuming processes"
    echo "INFO: Printing top 10 memory consuming processes"
    ps -eo pid,ppid,user,rss,comm --sort=rss | tail -10 
fi

closed as off-topic by Sylvain Pineau, Pilot6, karel, user364819, Eric Carvalho Jul 15 '15 at 13:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Sylvain Pineau, Pilot6, karel, Community, Eric Carvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

5

You can use the subprocess module :

#!/usr/bin/env python2
import subprocess
command_1 = 'ps -eo pid,ppid,user,rss,comm --sort=rss'.split()
command_2 = 'tail -10'.split()
first = subprocess.Popen(command_1, stdout=subprocess.PIPE)
subprocess.call(command_2, stdin=first.stdout)
  • command_1 is the command on the left of the pipe, split on spaces to make a list which is required by subprocess.Popen class

  • command_2 is the command on the left of the pipe split similarly to command_1

  • first is an object of the subprocess.Popen class, subprocess.Popen executes the command_1 and send its STDOUT to PIPE so that we can use it as an input to command_2

  • subprocess.call will execute the command_2 with its STDIN coming from the STDOUT of first

For ps -C kswapd0 -C kswapd1 -C kswapd2 -C kswapd3 -C kswapd4 -o pid,ppid,stime,etime,time,pcpu,args command you can do :

#!/usr/bin/env python2
import subprocess
command = 'ps -C kswapd0 -C kswapd1 -C kswapd2 -C kswapd3 -C kswapd4  -o pid,ppid,stime,etime,time,pcpu,args'.split()
subprocess.call(command)

Read the official documentation on subprocess to get more idea.

3

I'd use something like psutil to grab the data programmatically.

That's going to take a little while to learn so the quicker, scrappier alternative is to run the commands through a subshell-style construct from Python.

import commands
exit_code, output = commands.getstatusoutput('ps -eo pid,ppid,user,rss,comm --sort=rss')
print '\n'.join(output.split('\n')[-10:])

I've moved the tail into pure Python there, just for fun.

Not the answer you're looking for? Browse other questions tagged or ask your own question.