Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to figure out a way to set up a workstation to shut down automaticaly if the user as not been active on it for a given period of time.
There might be an ap to do this but all I've found are things like powersave which seem to be geared for laptops using acpi/apm amd things geared towards working with a UPS.
I've done something similar to shut down a printer/scanner/fileserver when nobody's up on the lan and there are no jobs scheduled to run, but that was simple using ping to test workstations and grep'ing for at jobs.
This time around I'm not sure what I should be checking for except to see if mplayer or xine is running (they might be watching a dvd or timeshift).
I can sort out the script. I just need some suggestions on what to look for (perhaps with grep)on the workstation to indicate it is being used.
Can you check mouse idle time? If so, I would check that first, then check to see if some common apps are running (xine, mplayer, etc..). If idle for > x minutes, then check for xine or mplayer playing media. If not, shut down?
I figured on doing something like that but I haven't been able to sort out what/where the info would be on my system and if I could just do it with existing cli tools and a bash script.
I found something called doinkd which looks like it might do the trick. but I was hoping to do this without running a daemon although this ap is in C and probably is no worse when it sleeps than if I used sleep in a bash script
I could use a screensaver but that's set to screensaver time. I wanted to be independent of that. The short script below will do the trick with a delay between tests of, say, 40 minutes. I'll also have to test to be sure my daughter is not just watching something with Codeine or Kaffeine, hence no mouse movements. Script would need to be fleshed out a bit. I'd probably run it as a cron job.
#!/bin/bash
# mouseup rm200702018
# A script for testing user activity on mouse
# and shutting down if none. Run as root
TIME=10m #set time between checks, "m" suffix for minutes
# Get a value for mouse interrupt which is 12: on this box.
# Use awk to get the value which is in line 12: column 2.
MOUSE1=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
# Put the value into a log file.
echo "`date` MOUSE1 equals $MOUSE1" > testing.txt
# Wait for [$TIME] before checking again.
sleep $TIME
# Get a another value for mouse interrupt [$TIME] minutes later
MOUSE2=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
# Send value to the log
echo "`date` MOUSE2 equals $MOUSE2" >> testing.txt
# If both values are the same log it then shut down
if [ $MOUSE1 -eq $MOUSE2 ] ; then
echo "`date` shutdown -h now" >> testing.txt
shutdown -h now
fi
#!/bin/bash
# mouseup rm200702018
# A script for testing user activity on mouse
# and shutting down if none. Run as root
TIME=10m #set time between checks, "m" suffix for minutes
MOUSE_CHECK=`awk '/[Mm]ouse/ {print $2}' /proc/interrupts`
while true
do
# Get a value for mouse interrupt which is 12: on this box.
# Use awk to get the value which is in line 12: column 2.
MOUSE1=$MOUSE_CHECK
# Put the value into a log file.
echo "`date` MOUSE1 equals $MOUSE1" > testing.txt
# Wait for [$TIME] before checking again.
sleep $TIME
# Get a another value for mouse interrupt [$TIME] minutes later
MOUSE2=$MOUSE_CHECK
# Send value to the log
echo "`date` MOUSE2 equals $MOUSE2" >> testing.txt
# If both values are the same log it then shut down
# useful to quote variables when compare
if [ "$MOUSE1" -eq "$MOUSE2" ] ; then
echo "`date` shutdown -h now" >> testing.txt
shutdown -h now
fi
done
#End of script
Yup that'd work.I had considered using a loop but hadn't thought through if there'd be any disadvantages. I knew it would work without problems as a cron job.
Almost thought about putting some of the if statements in as functions but I'm lazy and it's time to call it a day
#!/bin/bash
# /usr/local/mouseup rm200702018
# A script for testing user activity on their mouse
# and shutting down if given programs are not running
# and there has been no mouse activity for declared period
# off time. Run as root
TIME=$1m #set time between checks, "m" suffix for minutes
LOGFILE=/var/log/mouseup.log #this is where all activity will be logged
test -n "$TIME"
if [ $? -eq 1 ]; then
echo -e "\nYou have to declare how many minutes delay"
echo -e "between 1st and 2nd check for mouse activity.\n"
echo -e "Usage: mouseup [minutes]\n"
exit
fi
echo "`date` Starting mousup. Delay time set to $TIME." > $LOGFILE
# By using single ">" a new log is created. All others are ">>" adding new lines to existing logfile.
while true
do
MOUSE1=`cat /proc/interrupts | grep 12: | awk '{print $2}'`
echo "`date` MOUSE1 equals $MOUSE1" >> $LOGFILE
sleep $TIME
ps -A | grep "codeine" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
CODEINE_CHK=0
echo "`date` Codeine is running" >> $LOGFILE
else
CODEINE_CHK=1
echo "`date` Codeine is not running." >> $LOGFILE
fi
ps -A | grep "kaffeine" > /dev/null 2>&1
if [ $? -eq 0 ] ; then
KAFFEINE_CHK=0
echo "`date` Kaffeine is running" >> $LOGFILE
else
KAFFEINE_CHK=1
echo "`date` Kaffeine is not running." >> $LOGFILE
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.