Get time of the last session in seconds
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm getting the time of the last session of an specific user using the last
command like below:
last -aiF -n 1 fakeuser
The output of this command is something like:
fakeuser pts/0 Tue Nov 27 11:03:19 2018 - Tue Nov 27 11:14:57 2018 (00:11) 999.999.99.999
My question here is about the session time ((00:11)
in the example). Are there any way to get this time in seconds?
I know that I can calculate it based on the login and logout time, but I'm looking for some solution directly in the last
command, since I didn't found anything in the man
entrance for the command neither in the web.
FYI, I'm working with Debian 9.5, but I believe that some solution for it should work in any distribution.
Side problem for the perfect answer (I identified it after the first answer here): How to identify the sessions that lasted less than a minute?
linux time session last
add a comment |
up vote
0
down vote
favorite
I'm getting the time of the last session of an specific user using the last
command like below:
last -aiF -n 1 fakeuser
The output of this command is something like:
fakeuser pts/0 Tue Nov 27 11:03:19 2018 - Tue Nov 27 11:14:57 2018 (00:11) 999.999.99.999
My question here is about the session time ((00:11)
in the example). Are there any way to get this time in seconds?
I know that I can calculate it based on the login and logout time, but I'm looking for some solution directly in the last
command, since I didn't found anything in the man
entrance for the command neither in the web.
FYI, I'm working with Debian 9.5, but I believe that some solution for it should work in any distribution.
Side problem for the perfect answer (I identified it after the first answer here): How to identify the sessions that lasted less than a minute?
linux time session last
Just to clarify, you say "I'm looking for some solution directly in thelast
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines oflast | some code
) would be unacceptable ?
– Jeff Schaller
Dec 4 at 19:12
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm getting the time of the last session of an specific user using the last
command like below:
last -aiF -n 1 fakeuser
The output of this command is something like:
fakeuser pts/0 Tue Nov 27 11:03:19 2018 - Tue Nov 27 11:14:57 2018 (00:11) 999.999.99.999
My question here is about the session time ((00:11)
in the example). Are there any way to get this time in seconds?
I know that I can calculate it based on the login and logout time, but I'm looking for some solution directly in the last
command, since I didn't found anything in the man
entrance for the command neither in the web.
FYI, I'm working with Debian 9.5, but I believe that some solution for it should work in any distribution.
Side problem for the perfect answer (I identified it after the first answer here): How to identify the sessions that lasted less than a minute?
linux time session last
I'm getting the time of the last session of an specific user using the last
command like below:
last -aiF -n 1 fakeuser
The output of this command is something like:
fakeuser pts/0 Tue Nov 27 11:03:19 2018 - Tue Nov 27 11:14:57 2018 (00:11) 999.999.99.999
My question here is about the session time ((00:11)
in the example). Are there any way to get this time in seconds?
I know that I can calculate it based on the login and logout time, but I'm looking for some solution directly in the last
command, since I didn't found anything in the man
entrance for the command neither in the web.
FYI, I'm working with Debian 9.5, but I believe that some solution for it should work in any distribution.
Side problem for the perfect answer (I identified it after the first answer here): How to identify the sessions that lasted less than a minute?
linux time session last
linux time session last
edited Dec 6 at 19:06
asked Dec 4 at 18:32
James
1086
1086
Just to clarify, you say "I'm looking for some solution directly in thelast
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines oflast | some code
) would be unacceptable ?
– Jeff Schaller
Dec 4 at 19:12
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44
add a comment |
Just to clarify, you say "I'm looking for some solution directly in thelast
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines oflast | some code
) would be unacceptable ?
– Jeff Schaller
Dec 4 at 19:12
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44
Just to clarify, you say "I'm looking for some solution directly in the
last
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines of last | some code
) would be unacceptable ?– Jeff Schaller
Dec 4 at 19:12
Just to clarify, you say "I'm looking for some solution directly in the
last
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines of last | some code
) would be unacceptable ?– Jeff Schaller
Dec 4 at 19:12
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
If I recall correctly, the field you are interested in is usually in the format Days+HH:MM
, to convert that into seconds we can do something like this:
last -aiF -n 1 fakeuser |
awk 'gsub(/(' |
awk -F'[:+]' 'length($0) != 0 if(length($3) == 0) $3=$2; $2=$1; $1=0 print ($1 * 86400) + ($2 * 3600) + ($3 * 60)'
The first line is your last
command.
In the second line we isolate the field you want which appears to be field 14 from your command output.
The third line has all the action. The delimiter is set (-F
) to split fields on the plus (+
) and colon (:
) signs. We then test to make sure the line is not empty (length($0) != 0
). The next bit (if(length($3) == 0) $3=$2; $2=$1; $1=0
) is a quick trick to normalize any line into three fields, days, hours, and minutes. The rest (print ($1 * 86400) + ($2 * 3600) + ($3 * 60)
) is simply the conversion into seconds (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute).
There may be an easier way, but this is what I came up with while messing with this over my lunch.
It works fine (with some adaptations) for most of my cases, thank you for remember me about allawk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?
– James
Dec 6 at 13:50
1
I guess you could assume that if there is an entry that shows00:00
, that it would always be less than a minute. If the user shows up inlast
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?
– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the00:00
result, but it looks like a limitation of thelast
command itself, not exactly an edge case.
– James
Dec 6 at 19:06
add a comment |
up vote
1
down vote
GracefulRestart's awk solution to the question is excellent; as you noted in your comments to his answer the last
command isn't quite granular enough. But, assuming that auth.log
on debian is similar enough to what I see on Ubuntu you could so some maths based on the data there.
I appreciate that this is not an answer, but it still may be useful to you.
I filtered a bunch of lines out of /var/log/auth.log
, they pertain to both local and ssh logins.
cat auth.log
2018-12-06T07:28:00.487714+13:00 server systemd-logind[944]: New session 2597 of user tink.
2018-12-06T08:34:16.360766+13:00 server login[29537]: pam_unix(login:session): session opened for user tink by LOGIN(uid=0)
2018-12-06T08:34:16.372714+13:00 server systemd-logind[944]: New session c4 of user tink.
2018-12-06T08:34:20.960596+13:00 server login[29537]: pam_unix(login:session): session closed for user tink
2018-12-06T08:36:01.197712+13:00 server systemd-logind[944]: Removed session 2597.
Here's a (convoluted) awk-script ..
cat session.awk
if( $0 ~ /systemd-logind.+New session/ && $0~user )
start[$6]=$1
if( $0 ~ /systemd-logind.+ Removed session/ && start[gensub(/([0-9]+).*/, "\1", "1", $6)] != "" )
tmp = start[gensub(/([0-9]+).*/, "\1", "1", $6)]
cmd = "date +%s -d ";
cmd $1
if( $0 ~ /login.+ session opened/ && $0~user )
start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]=$1
if( $0 ~ /login.* session closed/ )
tmp = start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]
cmd = "date +%s -d ";
cmd $1
Running that against the snippet above:
awk -v user=tink -f session.awk sessions
tink was logged in for 4 seconds
tink was logged in for 4081 seconds
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485965%2fget-time-of-the-last-session-in-seconds%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If I recall correctly, the field you are interested in is usually in the format Days+HH:MM
, to convert that into seconds we can do something like this:
last -aiF -n 1 fakeuser |
awk 'gsub(/(' |
awk -F'[:+]' 'length($0) != 0 if(length($3) == 0) $3=$2; $2=$1; $1=0 print ($1 * 86400) + ($2 * 3600) + ($3 * 60)'
The first line is your last
command.
In the second line we isolate the field you want which appears to be field 14 from your command output.
The third line has all the action. The delimiter is set (-F
) to split fields on the plus (+
) and colon (:
) signs. We then test to make sure the line is not empty (length($0) != 0
). The next bit (if(length($3) == 0) $3=$2; $2=$1; $1=0
) is a quick trick to normalize any line into three fields, days, hours, and minutes. The rest (print ($1 * 86400) + ($2 * 3600) + ($3 * 60)
) is simply the conversion into seconds (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute).
There may be an easier way, but this is what I came up with while messing with this over my lunch.
It works fine (with some adaptations) for most of my cases, thank you for remember me about allawk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?
– James
Dec 6 at 13:50
1
I guess you could assume that if there is an entry that shows00:00
, that it would always be less than a minute. If the user shows up inlast
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?
– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the00:00
result, but it looks like a limitation of thelast
command itself, not exactly an edge case.
– James
Dec 6 at 19:06
add a comment |
up vote
1
down vote
accepted
If I recall correctly, the field you are interested in is usually in the format Days+HH:MM
, to convert that into seconds we can do something like this:
last -aiF -n 1 fakeuser |
awk 'gsub(/(' |
awk -F'[:+]' 'length($0) != 0 if(length($3) == 0) $3=$2; $2=$1; $1=0 print ($1 * 86400) + ($2 * 3600) + ($3 * 60)'
The first line is your last
command.
In the second line we isolate the field you want which appears to be field 14 from your command output.
The third line has all the action. The delimiter is set (-F
) to split fields on the plus (+
) and colon (:
) signs. We then test to make sure the line is not empty (length($0) != 0
). The next bit (if(length($3) == 0) $3=$2; $2=$1; $1=0
) is a quick trick to normalize any line into three fields, days, hours, and minutes. The rest (print ($1 * 86400) + ($2 * 3600) + ($3 * 60)
) is simply the conversion into seconds (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute).
There may be an easier way, but this is what I came up with while messing with this over my lunch.
It works fine (with some adaptations) for most of my cases, thank you for remember me about allawk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?
– James
Dec 6 at 13:50
1
I guess you could assume that if there is an entry that shows00:00
, that it would always be less than a minute. If the user shows up inlast
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?
– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the00:00
result, but it looks like a limitation of thelast
command itself, not exactly an edge case.
– James
Dec 6 at 19:06
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If I recall correctly, the field you are interested in is usually in the format Days+HH:MM
, to convert that into seconds we can do something like this:
last -aiF -n 1 fakeuser |
awk 'gsub(/(' |
awk -F'[:+]' 'length($0) != 0 if(length($3) == 0) $3=$2; $2=$1; $1=0 print ($1 * 86400) + ($2 * 3600) + ($3 * 60)'
The first line is your last
command.
In the second line we isolate the field you want which appears to be field 14 from your command output.
The third line has all the action. The delimiter is set (-F
) to split fields on the plus (+
) and colon (:
) signs. We then test to make sure the line is not empty (length($0) != 0
). The next bit (if(length($3) == 0) $3=$2; $2=$1; $1=0
) is a quick trick to normalize any line into three fields, days, hours, and minutes. The rest (print ($1 * 86400) + ($2 * 3600) + ($3 * 60)
) is simply the conversion into seconds (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute).
There may be an easier way, but this is what I came up with while messing with this over my lunch.
If I recall correctly, the field you are interested in is usually in the format Days+HH:MM
, to convert that into seconds we can do something like this:
last -aiF -n 1 fakeuser |
awk 'gsub(/(' |
awk -F'[:+]' 'length($0) != 0 if(length($3) == 0) $3=$2; $2=$1; $1=0 print ($1 * 86400) + ($2 * 3600) + ($3 * 60)'
The first line is your last
command.
In the second line we isolate the field you want which appears to be field 14 from your command output.
The third line has all the action. The delimiter is set (-F
) to split fields on the plus (+
) and colon (:
) signs. We then test to make sure the line is not empty (length($0) != 0
). The next bit (if(length($3) == 0) $3=$2; $2=$1; $1=0
) is a quick trick to normalize any line into three fields, days, hours, and minutes. The rest (print ($1 * 86400) + ($2 * 3600) + ($3 * 60)
) is simply the conversion into seconds (86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute).
There may be an easier way, but this is what I came up with while messing with this over my lunch.
answered Dec 4 at 19:32
GracefulRestart
1,08427
1,08427
It works fine (with some adaptations) for most of my cases, thank you for remember me about allawk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?
– James
Dec 6 at 13:50
1
I guess you could assume that if there is an entry that shows00:00
, that it would always be less than a minute. If the user shows up inlast
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?
– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the00:00
result, but it looks like a limitation of thelast
command itself, not exactly an edge case.
– James
Dec 6 at 19:06
add a comment |
It works fine (with some adaptations) for most of my cases, thank you for remember me about allawk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?
– James
Dec 6 at 13:50
1
I guess you could assume that if there is an entry that shows00:00
, that it would always be less than a minute. If the user shows up inlast
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?
– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the00:00
result, but it looks like a limitation of thelast
command itself, not exactly an edge case.
– James
Dec 6 at 19:06
It works fine (with some adaptations) for most of my cases, thank you for remember me about all
awk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?– James
Dec 6 at 13:50
It works fine (with some adaptations) for most of my cases, thank you for remember me about all
awk
possibilities. Anyway, here is a side problem: Are there any way to know this time if the session lasted less than a minute?– James
Dec 6 at 13:50
1
1
I guess you could assume that if there is an entry that shows
00:00
, that it would always be less than a minute. If the user shows up in last
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?– GracefulRestart
Dec 6 at 18:05
I guess you could assume that if there is an entry that shows
00:00
, that it would always be less than a minute. If the user shows up in last
, they were logged in for more than zero seconds. If the result of the awk script is zero minutes, that would indicate they were logged in for less than 60 seconds. Am I missing an edge case?– GracefulRestart
Dec 6 at 18:05
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the
00:00
result, but it looks like a limitation of the last
command itself, not exactly an edge case.– James
Dec 6 at 19:06
I was thinking about some way to find how many seconds has the user session taken, for sessions with less than a minute. In these cases, the last command gave me the
00:00
result, but it looks like a limitation of the last
command itself, not exactly an edge case.– James
Dec 6 at 19:06
add a comment |
up vote
1
down vote
GracefulRestart's awk solution to the question is excellent; as you noted in your comments to his answer the last
command isn't quite granular enough. But, assuming that auth.log
on debian is similar enough to what I see on Ubuntu you could so some maths based on the data there.
I appreciate that this is not an answer, but it still may be useful to you.
I filtered a bunch of lines out of /var/log/auth.log
, they pertain to both local and ssh logins.
cat auth.log
2018-12-06T07:28:00.487714+13:00 server systemd-logind[944]: New session 2597 of user tink.
2018-12-06T08:34:16.360766+13:00 server login[29537]: pam_unix(login:session): session opened for user tink by LOGIN(uid=0)
2018-12-06T08:34:16.372714+13:00 server systemd-logind[944]: New session c4 of user tink.
2018-12-06T08:34:20.960596+13:00 server login[29537]: pam_unix(login:session): session closed for user tink
2018-12-06T08:36:01.197712+13:00 server systemd-logind[944]: Removed session 2597.
Here's a (convoluted) awk-script ..
cat session.awk
if( $0 ~ /systemd-logind.+New session/ && $0~user )
start[$6]=$1
if( $0 ~ /systemd-logind.+ Removed session/ && start[gensub(/([0-9]+).*/, "\1", "1", $6)] != "" )
tmp = start[gensub(/([0-9]+).*/, "\1", "1", $6)]
cmd = "date +%s -d ";
cmd $1
if( $0 ~ /login.+ session opened/ && $0~user )
start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]=$1
if( $0 ~ /login.* session closed/ )
tmp = start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]
cmd = "date +%s -d ";
cmd $1
Running that against the snippet above:
awk -v user=tink -f session.awk sessions
tink was logged in for 4 seconds
tink was logged in for 4081 seconds
add a comment |
up vote
1
down vote
GracefulRestart's awk solution to the question is excellent; as you noted in your comments to his answer the last
command isn't quite granular enough. But, assuming that auth.log
on debian is similar enough to what I see on Ubuntu you could so some maths based on the data there.
I appreciate that this is not an answer, but it still may be useful to you.
I filtered a bunch of lines out of /var/log/auth.log
, they pertain to both local and ssh logins.
cat auth.log
2018-12-06T07:28:00.487714+13:00 server systemd-logind[944]: New session 2597 of user tink.
2018-12-06T08:34:16.360766+13:00 server login[29537]: pam_unix(login:session): session opened for user tink by LOGIN(uid=0)
2018-12-06T08:34:16.372714+13:00 server systemd-logind[944]: New session c4 of user tink.
2018-12-06T08:34:20.960596+13:00 server login[29537]: pam_unix(login:session): session closed for user tink
2018-12-06T08:36:01.197712+13:00 server systemd-logind[944]: Removed session 2597.
Here's a (convoluted) awk-script ..
cat session.awk
if( $0 ~ /systemd-logind.+New session/ && $0~user )
start[$6]=$1
if( $0 ~ /systemd-logind.+ Removed session/ && start[gensub(/([0-9]+).*/, "\1", "1", $6)] != "" )
tmp = start[gensub(/([0-9]+).*/, "\1", "1", $6)]
cmd = "date +%s -d ";
cmd $1
if( $0 ~ /login.+ session opened/ && $0~user )
start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]=$1
if( $0 ~ /login.* session closed/ )
tmp = start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]
cmd = "date +%s -d ";
cmd $1
Running that against the snippet above:
awk -v user=tink -f session.awk sessions
tink was logged in for 4 seconds
tink was logged in for 4081 seconds
add a comment |
up vote
1
down vote
up vote
1
down vote
GracefulRestart's awk solution to the question is excellent; as you noted in your comments to his answer the last
command isn't quite granular enough. But, assuming that auth.log
on debian is similar enough to what I see on Ubuntu you could so some maths based on the data there.
I appreciate that this is not an answer, but it still may be useful to you.
I filtered a bunch of lines out of /var/log/auth.log
, they pertain to both local and ssh logins.
cat auth.log
2018-12-06T07:28:00.487714+13:00 server systemd-logind[944]: New session 2597 of user tink.
2018-12-06T08:34:16.360766+13:00 server login[29537]: pam_unix(login:session): session opened for user tink by LOGIN(uid=0)
2018-12-06T08:34:16.372714+13:00 server systemd-logind[944]: New session c4 of user tink.
2018-12-06T08:34:20.960596+13:00 server login[29537]: pam_unix(login:session): session closed for user tink
2018-12-06T08:36:01.197712+13:00 server systemd-logind[944]: Removed session 2597.
Here's a (convoluted) awk-script ..
cat session.awk
if( $0 ~ /systemd-logind.+New session/ && $0~user )
start[$6]=$1
if( $0 ~ /systemd-logind.+ Removed session/ && start[gensub(/([0-9]+).*/, "\1", "1", $6)] != "" )
tmp = start[gensub(/([0-9]+).*/, "\1", "1", $6)]
cmd = "date +%s -d ";
cmd $1
if( $0 ~ /login.+ session opened/ && $0~user )
start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]=$1
if( $0 ~ /login.* session closed/ )
tmp = start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]
cmd = "date +%s -d ";
cmd $1
Running that against the snippet above:
awk -v user=tink -f session.awk sessions
tink was logged in for 4 seconds
tink was logged in for 4081 seconds
GracefulRestart's awk solution to the question is excellent; as you noted in your comments to his answer the last
command isn't quite granular enough. But, assuming that auth.log
on debian is similar enough to what I see on Ubuntu you could so some maths based on the data there.
I appreciate that this is not an answer, but it still may be useful to you.
I filtered a bunch of lines out of /var/log/auth.log
, they pertain to both local and ssh logins.
cat auth.log
2018-12-06T07:28:00.487714+13:00 server systemd-logind[944]: New session 2597 of user tink.
2018-12-06T08:34:16.360766+13:00 server login[29537]: pam_unix(login:session): session opened for user tink by LOGIN(uid=0)
2018-12-06T08:34:16.372714+13:00 server systemd-logind[944]: New session c4 of user tink.
2018-12-06T08:34:20.960596+13:00 server login[29537]: pam_unix(login:session): session closed for user tink
2018-12-06T08:36:01.197712+13:00 server systemd-logind[944]: Removed session 2597.
Here's a (convoluted) awk-script ..
cat session.awk
if( $0 ~ /systemd-logind.+New session/ && $0~user )
start[$6]=$1
if( $0 ~ /systemd-logind.+ Removed session/ && start[gensub(/([0-9]+).*/, "\1", "1", $6)] != "" )
tmp = start[gensub(/([0-9]+).*/, "\1", "1", $6)]
cmd = "date +%s -d ";
cmd $1
if( $0 ~ /login.+ session opened/ && $0~user )
start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]=$1
if( $0 ~ /login.* session closed/ )
tmp = start[gensub(/[^0-9]+([0-9]+).*/,"\1","1",$3)]
cmd = "date +%s -d ";
cmd $1
Running that against the snippet above:
awk -v user=tink -f session.awk sessions
tink was logged in for 4 seconds
tink was logged in for 4081 seconds
answered Dec 7 at 1:27
tink
4,08411218
4,08411218
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485965%2fget-time-of-the-last-session-in-seconds%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just to clarify, you say "I'm looking for some solution directly in the
last
command" -- does that mean you'd be satisfied with a (potential) answer of "You can't", and that a post-processing answer (along the lines oflast | some code
) would be unacceptable ?– Jeff Schaller
Dec 4 at 19:12
@JeffSchaller answers including some post-processing are totally fine. I didn't found any command to do it
– James
Dec 4 at 20:44