LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Need to shutdown after LAN inactivity (https://www.linuxquestions.org/questions/linux-software-2/need-to-shutdown-after-lan-inactivity-526941/)

ala_frosty 02-09-2007 12:38 AM

Need to shutdown after LAN inactivity
 
Hi:

I'd like to set up a Linux box (CentOS) to shutdown after a period of LAN inactivity in the interest of saving some cash on electricity and some ice on carbon dioxide emissions. I've done a bunch of googling looking to see how to shutdown the computer. I think /sbin/halt will stop it, but I don't know how to set up something else to run the command at the appropriate time.

I can restart the computer remotely with Wake-on-LAN (WOL) but I need the piece to shut it down automatically. In the worst case scenario, I guess I could run a cron job every half an hour, but I'd still need to know what to look at.

Suggestions? Comments? Rants?

Thanks,

Ess

unSpawn 02-09-2007 07:03 AM

shutdown after a period of LAN inactivity
I think you first need to define what counts as "inactivity":
- is there a period the box is less likely to be used (say working hours)?
- what services does the box run?
- do regular users only login remotely or locally as well?
- is it OK to still shut down if all, some or specific processes make use of the box?

If you for instance have only remote users you could use the amount of open network connections to determine a shutdown. Silly example:
Code:

#!/bin/sh --
# Unset debugging and exit-on-error after checking it works as advertised.
set -xe
# Get amount of connections.
CONN=`/bin/netstat -ntu 2>/dev/null| grep -c -v LIST 2>/dev/null`
# Check if var has contents, then scrub netstat headers (x2), else just exit.
if [ -n "$CONN" ]; then CONN=$[${CONN}-2]; else exit 1; fi
# If zero connections, initiate shutdown sequence with a 10 minute warning, else do nothing.
case "$CONN" in 0) /sbin/shutdown -h `date --date='+10 minutes' '+%H:%M'`;;
*) ;; esac
exit 0

This cronjob is extendible with checks for which users are logged in, processes running and any other filter you can think of. Still YMMV(VM).

ala_frosty 02-10-2007 11:13 AM

I'm not terribly knowledgeable about all this, but it looks like if the cron job runs and there are no users at that time, the server will shut down. This approach examines a snapshot of the server's performance/duty.

What I was thinking of was more along the lines of an activity review. I don't have a lot of users on the network, so there may be periods of up to an hour, say, when there is no network activity, but someone might still wish to use it. It is acting as an internal file server and web server.

I suppose that to accomplish this, I would need to be able to look at a log of something(???) that would indicate the pages served by apache and the files served by samba, and user logins (if someone is logged in via SSH, I don't want to boot them). If there was no acitivity during the past hour, then the box could shut down. I think I'd set the script up to run every fifteen minutes.

Does anyone have any suggestions along these lines?

Thank you


All times are GMT -5. The time now is 01:47 PM.