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
  • 70,465
Tat Mush
  • 41
  • 1
  • 2
  • 1
    Using time before any command tells the execution time it took (in minutes and seconds). – Kulfy Dec 19 '18 at 10:41
  • Maybe take a look at the acct package, it is used for process accounting, and logs start- and run-time among other info. – Soren A Dec 19 '18 at 11:54

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • And how to get starttime if process has already stop'ed ? – Soren A Dec 19 '18 at 11:39
  • 1
    @SorenA That's probably the core of the issue that should be explained. Linux system does not log such information. If a process is running - you can query information about it. Once it exits - its information is gone. – Sergiy Kolodyazhnyy Dec 19 '18 at 11:43
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