HTF
HTF

Reputation: 7260

BASH: subtract date to get a runtime

I have a backup that starts at midnight and sends an email when it's completed. I would like to check the runtime based on that:

Log format (/var/log/maillog):

Oct  4 08:23:35 test postfix/smtp[5351]: C5BBB2115C: to=<[email protected]>, relay=aspmx.l.google.com[74.125.206.26]:25, delay=30213, delays=30212/0.06/0.22/0.46, dsn=2.0.0, status=sent (250 2.0.0 OK 1412407415 q20si4382781wie.36 - gsmtp)

I'm not sure how I can subtract completion time from midnight to receive a runtime (it doesn't have to be awk if that helps)?

Example:

# awk '/[email protected]/{ printf ("%s%s%s\n", "Completed at: ", $3, " Runtime: ") }' /var/log/maillog
Completed at: 17:45:52 Runtime: 17h:45m:52s
Completed at: 08:56:00 Runtime: 08h:56m:00s
Completed at: 07:18:32 Runtime: 07h:18m:32s
Completed at: 05:53:23 Runtime: 05h:53m:23s
Completed at: 06:03:24 Runtime: 06h:03m:24s
Completed at: 08:50:51 Runtime: 08h:50m:51s
Completed at: 08:23:35 Runtime: 08h:23m:35s

Upvotes: 0

Views: 72

Answers (1)

user3620917
user3620917

Reputation:

If you are sure that your backup takes less then 24 hours there is nothing to subtract. You can only format differently your output:

$ awk '/[email protected]/{ "date -d" $3 " +%Hh:%Mm:%Ss" | getline T; close("date"); printf ("%s%s%s\n", "Completed at: ", $3, " Runtime: " T) }'

Completed at: 08:23:35 Runtime: 08h:23m:35s

I have used "date" | getline T construct here to assign the output of date to variable T and fulfill the task within your awk one-liner.

However if your backup can take > 24h then you have to give starting point as a second input (it is not present in your question so far) and then convert both dates with date --date="03/10/2014 08:23:35" +"%s" to number of seconds since 1970 and only then subtract.

Upvotes: 1

Related Questions