skdadle
skdadle

Reputation: 165

logging start and end time of a script that loops

I've got a .py file that has loops through a bash script like so:

 while True:
        text = "app cli -- some parameters -- email, password and body for a message"
        os.system(text)

I run it from bash and I can only stop this constantly sending out messages by typing in kill -9 pid in bash

and my question is, how could I log start and end time (end time meaning when I killed the process) and the amount of messages/loops (frequency) when I start it as a script and end it from bash.. would I have to adjust my python script or would I have to adjust my command at time of running? and how?

I'm also sure there is a more elegant way of doing what I'm trying to do, I'm just a beginner..

The only thing I managed to do was time how long it took to send out one message at a time.. and that doesn't really help..

Upvotes: 0

Views: 459

Answers (1)

rakwaht
rakwaht

Reputation: 3967

To log the time you can use the time command (Unix).

For example if I run this program:

iterations = 0
while True:
        // do something
        iterations+=1
        print iterations 

Call the script using the following command:

time python myscript.py

After that let's say you stop the execution using Cntr+C or any other forced stop. You can now check the amount of time you run the script and how many iteration you got by checking the output which in my case is:

7773
7774
7775
Traceback (most recent call last):
  File "script.py", line 5, in <module>
    print counter
KeyboardInterrupt


real    0m2.447s
user    0m0.109s
sys     0m0.234s

Notice you can find in the documentation of time command the meaning of the three time values you get from it

Upvotes: 1

Related Questions