Search code examples
pythonlinuxbashcsvtiming

executing a command from a string and timing it


I need to write a script that will receive several parameters, from which the most important one
Is a string that contains a command (in linux).

I need to be able to run it, keep the output in STDOUT (the usual), but also time it, and later output some .csv file.

Say it looks something like this:

timing_script.py param "echo hello world; cat /tmp/foo_bar"

The command will output stuff to STDOUT every couple of milliseconds, which I need it to stay there. I'm saying this because my previous attempt at this script was in bash and I had to cut from the time command to actually time that, which also meant having to disregard the output of the command.

I'll also have to append something like param,0.345 to a csv file.

How do I execute a command from a string and also time it?


Solution

  • You can use subprocess to run linux command from string and time to calculate execution time:

    import time
    from subprocess import Popen, PIPE
    
    start = time.time()
    
    p1 = Popen(["my_linux_cmd"], stdout=PIPE)
    print(p1.communicate()) # sdout
    
    end = time.time()
    exec_time = end - start
    print(exec_time) # exeution time
    

    Check subprocess.Popen fro more details about the available options

    Warning: to print the stdout you can also use Popen.stdout.read but use communicate() rather to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.