Invoking Unix commands from a Python app

R

Rob Cowie

Hi all,

An idea popped into my head recently for an app that would track how
much time a user spends in a particular piece of software (or at least,
for how long an application is open).

I'm assuming there is a way to do this via the command line and a unix
app, although I haven't yet invesitgated it.

My question is, can a command line application be invoked by a python
program? If so, how does one pass parameters to it and retrieve its
response?

Cheers and Merry Christmas,

Rob Cowie
 
R

Rob Cowie

Ok, I know see that os.spawnl() will suffice. However, how do I
retrieve the output of the command.

For example,

import os
os.spawnl(os.P_WAIT, '/bin/date')

Successfully executes the 'date' app, but I am unaware of how to get
its output
 
S

Sybren Stuvel

Rob Cowie enlightened us with:
Ok, I know see that os.spawnl() will suffice. However, how do I
retrieve the output of the command.

Apparently, os.spawnl() didn't suffice. Check out the popen2 module
and Popen* classes.

Sybren
 
R

Rob Cowie

Excellent... just the thing I was looking for. Thanks.

Does anyone know of a unix app that could be used to monitor the
duration of processes etc.?

Would 'top' do the trick?

Rob C
 
M

Martin Blume

Excellent... just the thing I was looking for. Thanks.

Does anyone know of a unix app that could be used to
monitor the duration of processes etc.?
man -k account showed me (among others):
acct (2) - switch process accounting on or off
acct (5) - execution accounting file

a short program to start accounting:
(warning: just hacked together)

smail:/home/mblume/wrk/tmp # cat acct.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>


int main(int argc, char *argv[])
{
char *pf;
struct stat st_buf;

if (strcmp(argv[1], "NULL") == 0)
{
pf = NULL;
printf("turning accounting off\n");
} // turn accounting off
else if (stat(argv[1], &st_buf) == -1)
{
printf("stat %s failed, error %d=%s\n",
argv[1], errno, strerror(errno));
return 1;
} // stat failed
else
{
// TBD check for a regular file
pf = argv[1];
printf("acct for %s\n", pf);
} // file seems ok

if (acct(pf) == -1)
{
printf("acct failed %d=%s\n",
errno, strerror(errno));
} // oops, acct failed

return 0;

} // main


HTH
Martin
 
W

Will McDonald

Excellent... just the thing I was looking for. Thanks.

Does anyone know of a unix app that could be used to monitor the
duration of processes etc.?

If you have control over starting the program then "time" will probaby suffice.

time - time a simple command or give resource usage

DESCRIPTION
The time command runs the specified program command with the given
arguments. When command finishes, time writes a message to standard
output giving timing statistics about this program run. These statis-
tics consist of (i) the elapsed real time between invocation and ter-
mination, (ii) the user CPU time (the sum of the tms_utime and
tms_cutime values in a struct tms as returned by times(2)), and (iii)
the system CPU time (the sum of the tms_stime and tms_cstime values in
a struct tms as returned by times(2)).

[wmcdonald@stella ~]$ time find . > /dev/null 2>&1

real 0m0.010s
user 0m0.001s
sys 0m0.009s
[wmcdonald@stella ~]$


Will.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top