Reputation: 38214
I have started processing of several files. I noted down the (clock) time when the processing started. It is taking too long to finish and I want to go to sleep, but I want to note down the time when the processing stops. I do not want a very precise time reading, clock time is okay with me. Is there any tool, or command or a small script that can help me achieve this.
Thanks for any help.
I am on an ubuntu 12.04 machine and running a single executable which processes multiple files.
Upvotes: 1
Views: 327
Reputation: 414685
You can find out when a process finishes for both new and already running processes using psutil
module (cross-platform). It might provide better precision when sleep()
-based solutions:
#!/usr/bin/env python
"""Print the finish time for a process specified by <pid>.
Usage:
process-finish-time <pid>
"""
import sys
from datetime import datetime
from psutil import NoSuchProcess, Process # pip install psutil
try:
pid = int(sys.argv[1])
except (IndexError, ValueError):
sys.exit(__doc__)
try:
p = Process(pid) # OR use `psutil.Popen()` to start a new process
except NoSuchProcess as e:
sys.exit(e)
name = None
while p.is_running():
name = p.name
p.wait() # block until the process ends
print("Process (%s) finished at %s UTC" % (name, datetime.utcnow()))
You can pass timeout
to p.wait()
if you want to do something while the process is running.
Upvotes: 1
Reputation: 38214
Finally I was able to write a python script which helped me out. The important requirement was that, i should be able to know the time (when the process stops) even when the process was already started. I hope the following script would help others who come across this question.
import datetime
import commands
import time
import sys
def main(argv):
if len(argv) < 2:
sys.stderr.write("Usage: %s <action>" % (argv[0],))
return 1
output = commands.getoutput("ps -e")
while argv[1] in output:
output = commands.getoutput("ps -e")
print argv[1], " running", datetime.datetime.now()
#it should be set depending upon the precision a user wants.
time.sleep(5)
sys.stderr.write("Process finished at %s\n" % datetime.datetime.now())
if __name__ == "__main__":
sys.exit(main(sys.argv))
Upvotes: 0
Reputation: 9889
You can use this tiny python script
import datetime
import subprocess
import time
import sys
p = subprocess.Popen(sys.argv[1:])
while p.poll() is None:
time.sleep(1)
sys.stderr.write("Process finished at %s\n" % datetime.datetime.now())
Example:
/tmp/watcher.py sleep 10
Process finished at 2012-08-19 10:38:11.233989
Upvotes: 1
Reputation: 2497
Time command might help you
Format
$> time <command>
For example : if its a bash script
$> time sh Test.sh
Output
real 0m0.004s
user 0m0.001s
sys 0m0.002s
Upvotes: 0