I'm an absolute scripting novice but I've been asked to create a script that checks how long a user has been logged in to a Linux server and if it's longer than, let's say, 5 hours, it would need an Exit status which can be picked up by our monitoring system and alert out.

I don't want someone to write the script for me (I'm no cheat), but I'd appreciate as much advice as I can get.

Has anyone had any experience with writing a shell script that would do this or something similar?

Many thanks in advance.

3 Answers 3


Look at the programs who and w and the files they use utmp and wtmp.

Instead of writing a program that interrogates the files themselves (where the information is held in a binary form), it is probably easier to script something that parses the output of one of those programs.

man who etc for more information.


Did you consider the last command?

  • last is the wrong command. The questioner wants to look at the active login sessions, not the log of login events. jdebp.uk./FGA/unix-login-database.html
    – JdeBP
    Jan 30, 2020 at 12:05
  • Not clear (to me, non-native speaker) from the request wording. last has the advantage of immediately giving the (recent) session duration.
    – RudiC
    Jan 30, 2020 at 12:13

This is everyday fare for Nagios plug-ins. If you are using something else, you will have to work out what behaviour the monitoring system requires of your plug-in. Nagios has a documented interface for what is written to standard output and what the plug-in's exit status should be.

One place to start with developing a Nagios plug-in is where people have already done some of the job. See, for example, Hari Sekhon's Nagios plug-in collection, where there is a check_users.sh script in the "older" section that looks at the active logins table but does not impose the rules that you want imposed. It's possible that someone else has developed something to do what you want already. There are numerous variants on and augmentations to the Nagios-supplied check_users, such as this, written by people over the years. Some of them are in Nagios Exchange.

You can see M. Sekhon's script constructing a $userlist string, by parsing the output of the who command. It's actually just one line. What you want is some more complex processing of the output of the who command than that, which you will have to write.

… Or a program in a programming language that can actually directly read the login database, rather than the somewhat rickety mechanism of parsing the human-readable (not intended as machine-parseable) output of the who/w command, and do the fairly trivial output generation and exit status calculation. Nagios plug-ins do not have to be written in shell script, and parsing the human-readable outputs of tools back into a machine-processable form is a notorious source of bugs on Unices and Linux operating systems. Combine that with the fact that you are going to be involving another notorious source of bugs, date/time arithmetic using human-readable forms, and the result is that shell script is going to be very fragile here.

Further reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .