Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Invoking Unix commands from a Python app

Reply
Thread Tools

Invoking Unix commands from a Python app

 
 
Rob Cowie
Guest
Posts: n/a
 
      12-16-2005
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

 
Reply With Quote
 
 
 
 
Rob Cowie
Guest
Posts: n/a
 
      12-16-2005
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

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      12-16-2005
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
 
Reply With Quote
 
Rob Cowie
Guest
Posts: n/a
 
      12-16-2005
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

 
Reply With Quote
 
Martin Blume
Guest
Posts: n/a
 
      12-16-2005
"Rob Cowie" schrieb
> 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


 
Reply With Quote
 
Will McDonald
Guest
Posts: n/a
 
      12-17-2005
On 16 Dec 2005 08:45:01 -0800, Rob Cowie <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating ActiveXobject in mozilla, invoking shell commands. Mohd Ebrahim ASP .Net 2 07-12-2010 08:21 AM
invoking system commands from a perl script vabby Perl Misc 10 03-13-2007 06:55 PM
Python and Unix Commands timdoyle05 Python 2 09-19-2005 10:46 AM
Unix commands within Python code Kory Wheatley Python 3 07-16-2004 01:03 PM
Invoking Commands in J2EE Jim Fisher Java 0 05-22-2004 12:08 PM



Advertisments