Invoking Unix commands from a Python app

Discussion in 'Python' started by Rob Cowie, Dec 16, 2005.

  1. Rob Cowie

    Rob Cowie Guest

    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
    Rob Cowie, Dec 16, 2005
    #1
    1. Advertising

  2. Rob Cowie

    Rob Cowie Guest

    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
    Rob Cowie, Dec 16, 2005
    #2
    1. Advertising

  3. 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
    Sybren Stuvel, Dec 16, 2005
    #3
  4. Rob Cowie

    Rob Cowie Guest

    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
    Rob Cowie, Dec 16, 2005
    #4
  5. Rob Cowie

    Martin Blume Guest

    "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
    Martin Blume, Dec 16, 2005
    #5
  6. On 16 Dec 2005 08:45:01 -0800, Rob Cowie <> 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.
    Will McDonald, Dec 17, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Fisher

    Invoking Commands in J2EE

    Jim Fisher, May 22, 2004, in forum: Java
    Replies:
    0
    Views:
    339
    Jim Fisher
    May 22, 2004
  2. Kory Wheatley

    Unix commands within Python code

    Kory Wheatley, Jul 15, 2004, in forum: Python
    Replies:
    3
    Views:
    11,975
    Jeremy Sanders
    Jul 16, 2004
  3. timdoyle05

    Python and Unix Commands

    timdoyle05, Sep 19, 2005, in forum: Python
    Replies:
    2
    Views:
    431
    Piet van Oostrum
    Sep 19, 2005
  4. Mohd Ebrahim
    Replies:
    2
    Views:
    900
    Cubaman
    Jul 12, 2010
  5. vabby
    Replies:
    10
    Views:
    221
    Joe Smith
    Mar 13, 2007
Loading...

Share This Page