Capture and save running process on Python 3.6

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Capture and save running process on Python 3.6

Ajinkya Bobade
Hello,

Hope someone can help me on this. In the following code, I want to capture
print('Recording GPS Position...')  on sd card, for now, this is printing
on terminal directly I want to capture this runtime process (p_1) from
terminal and store on sd card as it is being executed how do I do this.
Also in general what is a way to capture and store processes in runtime
from terminal and store on sd card(note I do want to store processes as it
is being executed and not after its execution is done)

import sys
import os
import time
import subprocess, shlex
import signal
import serial
import psutil
from subprocess import Popen, PIPE


def recording():
    flag = 0
    ser = serial.Serial('/dev/ttyACM0', 921600, timeout=1)
    ser.flushOutput()

    # ROSBAG Recordings (Shell commands that execute the messages on
the terminal)

    messages = 'rosbag record -o GPS_Position.bag
dji_sdk/gps_position', 'rosbag record -o IMU_Data.bag dji_sdk/imu',
'rosbag record -o Attitude.bag dji_sdk/attitude', 'rosbag record -o
Velodyne_Packets.bag velodyne_packets', 'rosbag record -o
Velodyne_Points.bag velodyne_points',  # rosbag record -o Velocity.bag
dji_sdk/velocity'

    while flag == 0:
        try:
            args1 = shlex.split(messages[0])  # messages[0] = rosbag
record -o GPS_Position.bag dji_sdk/gps_position
            #print (args1)
            p_1 = subprocess.Popen(args1, stdout=PIPE)
            print('Recording GPS Position...')
            p_1.stdout.flush()
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: Capture and save running process on Python 3.6

Noufal Ibrahim KV
On Thu, Nov 02 2017, Ajinkya Bobade wrote:

> Hello,
>
> Hope someone can help me on this. In the following code, I want to capture
> print('Recording GPS Position...')  on sd card, for now, this is printing
> on terminal directly I want to capture this runtime process (p_1) from
> terminal and store on sd card as it is being executed how do I do
> this.

You can either run your program with a stdout redirect e.g.

python foo.py > logfile.txt

or inside your program, replace sys.stdout with an open file object
pointing to your log file. I'd go with the former.

Secondly, if you want to capture logs and things, you should consider
using the logging module.

[...]

--
Cordially,
Noufal
http://nibrahim.net.in
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: Capture and save running process on Python 3.6

Ajinkya Bobade
I wrote a code which works for text files: This code redirects the
output to sd card it goes as follows:

import sys
oldstdout = sys.stdout     # So you can restore later
sys.stdout = open("/Volumes/aj/hello world.txt", 'w')  # Or whatever
your logfile is
for i in range(10):
    print ("Hello", i)
sys.stdout.close()
sys.stdout = oldstdout

However, I want to write this code for "*.bag" extension files that is
 sys.stdout = open("/Volumes/aj/hello world.bag", 'w') . This file is
defined as Binary(application/octect-stream). The above code doesn't
work in this scenario. How do I make it work?

On Thu, Nov 2, 2017 at 4:55 AM, Noufal Ibrahim KV
<[hidden email]> wrote:

> On Thu, Nov 02 2017, Ajinkya Bobade wrote:
>
>> Hello,
>>
>> Hope someone can help me on this. In the following code, I want to capture
>> print('Recording GPS Position...')  on sd card, for now, this is printing
>> on terminal directly I want to capture this runtime process (p_1) from
>> terminal and store on sd card as it is being executed how do I do
>> this.
>
> You can either run your program with a stdout redirect e.g.
>
> python foo.py > logfile.txt
>
> or inside your program, replace sys.stdout with an open file object
> pointing to your log file. I'd go with the former.
>
> Secondly, if you want to capture logs and things, you should consider
> using the logging module.
>
> [...]
>
> --
> Cordially,
> Noufal
> http://nibrahim.net.in
> _______________________________________________
> BangPypers mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/bangpypers
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers
Reply | Threaded
Open this post in threaded view
|

Re: Capture and save running process on Python 3.6

Noufal Ibrahim KV
On Thu, Nov 02 2017, Ajinkya Bobade wrote:


[...]

> However, I want to write this code for "*.bag" extension files that is
>  sys.stdout = open("/Volumes/aj/hello world.bag", 'w') . This file is
> defined as Binary(application/octect-stream). The above code doesn't
> work in this scenario. How do I make it work?

[...]

The contents of a file are not dependent on its extension. What is it
that you're trying to do here? I assumed that you simply want to capture
all your log messages.


--
Cordially,
Noufal
http://nibrahim.net.in
_______________________________________________
BangPypers mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/bangpypers