Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-22-2010, 09:59 AM   #1
LQ Newbie
Registered: Feb 2010
Posts: 3

Rep: Reputation: 0
how to find out if there is user activity on the system

I am working on a program lets say programX which must run when the computer is not in use. I want to develop a monitoring program to monitor if there is user activity on the system so that it can stop the programX from running when the user is using the system and start programX when there is no user activity. Is there a way to determine this in linux?

Much thanks
Old 02-22-2010, 10:11 AM   #2
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 288

Rep: Reputation: 36
You can always grep for users logging in in the /var/log/auth.log

or run a ps on a particular user id or even just using the "who" command

It does depend on what activity you are looking for, just logging in or actual programs being run.

Old 02-22-2010, 10:15 AM   #3
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
It would depend on what you meant by "user activity".

Many programs run automated things under a "user" account (often an Administrative "user"). For example if a "user" ran something in background with a nohup it would continue to run even after the "user" logged out. Cron and at jobs can be kicked off for "users".

If what you're really interested in is whether a "user" is logged on an "doing" something I'd suggest running "who" to get a list of user's logged in then running a "ps -fu<user>" for each "user" reported by who and see what they're running. Many user's leave themselves logged in so if all you see are shells (bash, ksh, csh etc...) and no other processes they *probably* aren't "active" but you can't guarantee it because you might have just happen to run your ps in between commands they've typed. If they're all using GUIs it might be even more complicated.

You can try setting shell timeouts globally (e.g. set TMOUT variable in /etc/profile) so that new logins automatically logout after a certain amount of inactivity. However, some users try to circumvent this by doing something like "vi test" so the shell thinks they're doing something. (The wouldn't show as inactive and timeout until after they'd closed the vi session.) Of course you could check when the process started and kill it based on age. (e.g. if they started a vi at 11 AM and it's still there at 11 PM either they're very dedicated, they're very lazy or trying to circumvent - I'd just kill it off at that point - if they weren't smart enough to save 12 hours worth of work in a vi document or to recover from the swap file afterwords I don't have a lot of sympathy for them.)

P.S. I'm not the BOFH but I do like the way he thinks.

Last edited by MensaWater; 02-22-2010 at 10:19 AM.
Old 02-23-2010, 08:40 PM   #4
LQ Newbie
Registered: Feb 2010
Posts: 3

Original Poster
Rep: Reputation: 0
I must apologize for my poor explanation of my problem, by user activity I mean a mouse movement or a keyboard button pressed. I want the program to run when users are not using the system, so when a user comes onto the system the monitoring program should be able to sense that initial mouse movement or key press and terminate. In other words can I do what the OS does when the screensaver is active and a user moves the mouse or presses a key? Is there a way in linux to catch those signals. I am very new to linux but if you can just point me in the right direction I would very much appreciate it.

Much Thank
Old 02-23-2010, 10:39 PM   #5
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Take a look at the source code for your screensaver. How does the screensaver determine that a specified time has elapsed without user activity before starting the screensaver?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can they find me without activity sbms Linux - Security 17 12-16-2008 12:43 AM
who is user nobody on my system and why is he running find? Kelean Debian 5 09-03-2007 09:17 AM
Keep track of logs to find suspicious activity leosgb Linux - Security 3 03-04-2006 07:09 PM
monitor user activity alagenchev Linux - Security 5 01-20-2006 11:02 PM
User activity on a given date twantrd Linux - General 0 12-13-2003 03:34 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration