RDK
RDK

Reputation: 385

Running a "piping" script from an os.system() command

I currently have a script ("monitor") which monitors and outputs various system-type data to the screen. I have a Python program which is post processing that information when I use this command to pipe the monitor output to my Python program.

/usr/local/bin/monitor | /mnt/usbkey/pgms/myMonitorPgm.py

This much is working just fine.

I would like to use an os.system command (or something else??) to run the monitor script and create the pipe from within my Python program. This would run when I start my Python program and thus eliminate the need the above piping format/command.

Is this possible?

Upvotes: 1

Views: 5640

Answers (2)

big-town
big-town

Reputation: 1

In my case what worked was:

with open('/tmp/mailbody', 'w') as f:
    f.write(body)
os.system('/usr/bin/heirloom-mailx -s ' + subject + ' ' + mail + '< /tmp/mailbody')

Upvotes: 0

Eric Renouf
Eric Renouf

Reputation: 14500

You should probably look at the subprocess module, in particular create a Popen object with stdout=PIPE then you can use communicate() or similar to read from the pipe within your Python program.

For example, you could do:

import subprocess
proc = subprocess.Popen( [ '/usr/local/bin/monitor' ], stdout=subprocess.PIPE )
stdout, _ = proc.communicate()

Now stdout will have the output from running the monitor program.

Upvotes: 3

Related Questions