4

I'm trying to figure out when a command I entered stopped executing, for example

sudo apt-get update

I want information like:

started execution at : 12:00
finished execution at : 12:15
Zanna
  • 72,312
Tat Mush
  • 41
  • 1
  • 2

2 Answers2

2

Key point that should be understood about processes is that their metadata is not logged by default. You can, however, obtain certain information while process runs.

As has been stated in related StackOverflow question in wlk's answer, start time can be obtained via -o flag to ps and ltime format specifier:

ps -eo pid,lstart,cmd

Stop time, however, is not tracked by the system usually, so that's something you'll have to track yourself. Knowing PID of a process, you could poll for that PID existence, and echo the timestamp when it disappears

while kill -s 0 $pid ; do : ; sleep 0.25 ; done && date

See also: https://stackoverflow.com/a/5207048/3701431

1

How about writing our own bash script?

#!/bin/bash

start_time=$(date +"%T")

$*                    

finish_time=$(date +"%T")

echo "started execution at : $start_time"
echo "finished execution at : $finish_time"

Save the script as logscript.sh

Make it executable by using chmod +x logscript.sh

You can run 'command' using ./logscript.sh command

For example, ./logscript.sh sudo apt-get update

Screenshot of the result

Check this site for interesting time-display formats.

NPsoft
  • 138