Invoking Unix commands from a Python app

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Cowie

    Invoking Unix commands from a Python app

    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

    #2
    Re: Invoking Unix commands from a Python app

    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

    Comment

    • Sybren Stuvel

      #3
      Re: Invoking Unix commands from a Python app

      Rob Cowie enlightened us with:[color=blue]
      > Ok, I know see that os.spawnl() will suffice. However, how do I
      > retrieve the output of the command.[/color]

      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

      Comment

      • Rob Cowie

        #4
        Re: Invoking Unix commands from a Python app

        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

        Comment

        • Martin Blume

          #5
          Re: Invoking Unix commands from a Python app

          "Rob Cowie" schrieb[color=blue]
          > 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.?
          >[/color]
          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


          Comment

          • Will McDonald

            #6
            Re: Invoking Unix commands from a Python app

            On 16 Dec 2005 08:45:01 -0800, Rob Cowie <cowie.rob@gmai l.com> wrote:[color=blue]
            > 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.?[/color]

            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@stell a ~]$ time find . > /dev/null 2>&1

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


            Will.

            Comment

            Working...