473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Dec 16 '05 #1
5 1271
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

Dec 16 '05 #2
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
Dec 16 '05 #3
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

Dec 16 '05 #4
"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
Dec 16 '05 #5
On 16 Dec 2005 08:45:01 -0800, Rob Cowie <co*******@gmail.com> 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.
Dec 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Justin Johnson | last post by:
I have a unix id that has group1 for a primary group, and group2, group3, and group4 for supplementary groups. I'd like to change my primary group before running certain commands. So sometimes I...
8
by: Neale | last post by:
In a python program, I want to do a Unix directory list and then append selected files to each other which, as you know, is just "cat filename2 >> filename1." How do I escape into the shell and...
3
by: Kory Wheatley | last post by:
Hi all, I've just started to learn Python and I have a question. What is the syntax to embed Unix commands, or call other programs within a Python script? for example I would like to use a...
8
by: Oli Schwarz | last post by:
Hello, how can I read out the uptime of a unix system in python (Linux and *BSD). I have not found a uptime-function in the Library. Regards Oli
2
by: timdoyle05 | last post by:
Hi, I have a question relating to how Unix commands can be issued from Python programs. Im am currently writing a large test script in python and I need this script to call three other separate...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
5
by: loial | last post by:
How can I delete hidden files on unix with python, i.e I want to do equivalent of rm .lock*
10
by: blaine | last post by:
Hey everyone, So I've got a quick query for advice. We have an embedded device in which we are displaying to an LCD device that sits at /dev/screen. This device is not readily available all...
2
by: sixtyfootersdude | last post by:
Hey all! Just doing some tinkering with python. I am running mac os x and I am wondering how I can call unix commands from python. In C I think I would use a fork, although it has been...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.