Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I was wondering if ubuntu (preferably 9.04) has something that can determine if the user has logon to his computer the first time, and more then one time on the same day.

The reason why I need this is to create a bash script which will launch an application (ie Firefox) if he had logged the first time on that day. But if he had log in again in the same day, it will not launch the application again.

Does anyone know if Ubuntu possess such a feature in a command line?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

This is how it works for me on a non-ubuntu linux machine that is handy at the moment.

last | grep "$(date | cut -c -10)" | grep $(whoami) | wc -l

You can tweak it a bit and get your result -- replace $(whoami) with the username of interst.


Update with cleanup based on comments.

last username | grep "$(date '+%a %b %e')" | wc -l | sed 's/ //g'

The sed filter at the end cleans out extra formatting whitespace in the resulting count.

share|improve this answer
1  
The whoami is not necessary if the user is known since last accepts a username as an argument. If it's a general purpose script (the user is not known until it's run) then you can do "last $(whoami)" and leave out the second grep. The cut is unnecessary since you can do "date '+%a %b %e'". You didn't say that the result of your command would be some number greater than or equal to zero that could be used in an "if". –  Dennis Williamson Jul 2 '09 at 5:04
    
The whoami was for sample code. I agree about the date formatting, the cut is a bit dirty. Using the last with username is also quite correct. The result of wc -l will count the number of times login was seen (alas, with a tab prefixed, so might need some cleanup for input to other places). –  nik Jul 2 '09 at 8:34

The last command will show a record of each login at a shell.

share|improve this answer

Something like this would probably work:

#!/bin/sh

CMD=`last -2 $USER | awk '{print $3,$4,$5}'`
THIS_DATE=`echo $CMD | head -1`
LAST_DATE=`echo $CMD | tail -1`

if [[ $THIS_DATE == $LAST_DATE ]]; then
        /usr/bin/firefox &
else
        # Do something else
fi
exit 0
share|improve this answer
    
last on my system prints a line saying when the wtmp file starts at the end, so tail wouldn't work unless you did "LAST_DATE=$(echo $CMD|head -2|tail -1)". But the bigger problem is that your answer gives the same result whether the user logged in once, twice or twelve times that day. –  Dennis Williamson Jul 2 '09 at 5:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.