0

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..

skdadle
  • 155
  • 1
  • 2
  • 17
  • "I'm also sure there is a more elegant way of doing what I'm trying to do" It's not clear to me what you're trying to do, can you please clarify the purpose of the script? – roganjosh Aug 29 '18 at 07:47
  • hey yes, basically wanna bombard our app server by constantly sending out messages from pi, to check its capacity and how it handles stress, thats why I need to log many things and frequency is important – skdadle Aug 29 '18 at 07:49
  • I wonder if [this](https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully) is of any use? I'm not too familiar with Linux – roganjosh Aug 29 '18 at 07:55

1 Answers1

1

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

rakwaht
  • 3,666
  • 3
  • 28
  • 45
  • thanks for that,could i possibly redirect the responses from server that appear in bash and the time command output to a log file, so far i've used this { time ./stresstest02.py ; } 2> time.txt but that only logs time. is there a way i could combine the 2? outputs – skdadle Aug 30 '18 at 07:59
  • For example `(time ls) > log.log 2>&1` does write the `time` output and the `ls` output in the same file. – rakwaht Aug 30 '18 at 08:31