LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-02-2009, 12:19 AM   #1
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Rep: Reputation: 17
Auto suspend for my home server


Hi Folks,
I'm just setting up my first home server with ubuntu and I would like the box to suspend to ram when no one is accessing it for a while (let's say for 1 hour)

The wake up task is already done and is working fine so far; but how can I put the machine to sleep automatically?

I guess I have to watch for network-traffic but have no clue how to do it.

Any ideas?
 
Old 06-02-2009, 12:38 AM   #2
Chris_no
LQ Newbie
 
Registered: Jun 2009
Distribution: Ubuntu 8.04 Hardy Heron
Posts: 15

Rep: Reputation: 0
You could write a script that cron runs regularly to check if there as been any activity in the last 5 min or whatever seems natural.

Something in the neighbourhood of

Code:
#!/bin/sh
#Activity check

#Select relevant info
$1 = ip neigh ls | grep (ip adress of choice)

#My recall of shell syntax sucks but you get the idea.

if $1==correct result

run suspend ram command

else

exit 0
 
Old 06-02-2009, 08:20 AM   #3
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Original Poster
Rep: Reputation: 17
Thank you, Chris.
I must confess in my now 5 Linux years I never heard of a command named "ip" but it sounds very promising.
Think I have to study "ip" for a while :-)

I guess it might be a good idea to go a bit deeper into analysis, e.g. ignoring broadcasts, so an idling windows box doesn't keep the server awake.

If someone could go a bit more into details I would be happy.
 
Old 06-02-2009, 03:43 PM   #4
Chris_no
LQ Newbie
 
Registered: Jun 2009
Distribution: Ubuntu 8.04 Hardy Heron
Posts: 15

Rep: Reputation: 0
If you want to learn about ip this is the place to go.

http://tldp.org/HOWTO/Adv-Routing-HOWTO/

Ip is the command that ifconfig, route, netstat etc rely on to do their dirty work.

Kinda like C relying on assembler...

I'm super busy at the moment and can't spare time to go into detail
but good luck with your network project!!
 
Old 06-05-2009, 06:46 AM   #5
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Original Poster
Rep: Reputation: 17
Phew! That's pretty hard stuff I don't really understand.
Isn't that site more on filtering? I haven's found any information on logging or monitoring traffic.

Anyway I have found a pretty easy solution using
# ethtool -S eth0
and a little perl script.

Now I can monitor the amount of traffic, unicast, multicast and braoadcast packets.

The sleep function works it pretty well now. when there is very low unicast traffic for 30 Minutes and no unicast traffic at all for 5 minutes the server suspends with powersave -u.
My problem now is that the Server wakes up after a really short time (less then a minute?) even when there is no other computer on (except of the Fritz!Box router).

The ethernet card is set up to wake the server on a WOL packet as well as on unicast packets with
# ethtool -s eth0 wol ug.

I have no clue how to find out why the server wakes up. Is there any log?
I'm using powersave -u to suspend.
 
Old 06-05-2009, 11:02 PM   #6
geesh
Member
 
Registered: Feb 2004
Posts: 52

Rep: Reputation: 16
heres a script i use in a freebsd ( FreeNas) server I use which works well.

Code:
#!/bin/bash
sleep 180     # Sleep for 3 mins to make sure the server don't shut down right away
class=192.168.2
flag=0
flag1=3       # Set the number of total failure before shutdown
SLEEP=720     # Numbers of seconds between each check/loop, right now 12 min
_ping_range() {
    cnt=0
    for i in {5..7}; # Set the range   192.168.2.5 through .7
    do
        /sbin/ping -c 1 -s 8 -t 1 ${class}.${i} 
        if [ $? -eq 0 ]; then let cnt++;fi
    done
    if [ $cnt -eq 0 ]; then
        return 0;
    else
        return 1
    fi
}
_shutdown() {  
    if [ $flag -eq $flag1 ];then 
        /sbin/shutdown -p now
        exit 0;
    fi
}

while : ;
do
    _ping_range
    if [ $? -eq 0 ];then
        let flag++;
        _shutdown;
    else
        flag=0;
    fi
    /bin/sleep $SLEEP;
done
I imagine it would work for linux also with minor changes . At the least it should give you someplace to start and some more ideas.


Try using only the wol magic packets for waking up.

Last edited by geesh; 06-05-2009 at 11:07 PM.
 
Old 06-06-2009, 08:36 PM   #7
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Original Poster
Rep: Reputation: 17
Yea, that's pretty simple.

Unfortunately the seems to be no solution for the inexplicable wakeup phenomena, so I had to disable the wakeup on unicast-packet receive. Now the system wakes up only on WOL package receive which is a lack of beauty.

Thanks for helping.
 
Old 06-10-2009, 10:32 PM   #8
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Original Poster
Rep: Reputation: 17
auto-shutdown script

For those who are interested:
I enhanced the script that geesh posted here.
Now it also watches for interruptible tasks running and tries to ping the last found host first. That way it avoids all that unsuccessfull pinging most of the time.

On my machine it works fine.

Code:
#! /bin/bash
sleep 180     # Sleep for 3 mins to make sure the server don't shut down right away
class=192.168.178
myIP=$(/sbin/ifconfig | egrep -o "192.168.178.[0-9]{1,3}")
failcount=0
maxfail=3       # Set the number of failure count before we do a shutdown
SLEEP=300       # Numbers of seconds between each check/loop, right now 5 min

LOGFILE=/var/log/autosuspend.log

# Don't suspend if one of the following processes is active (separate by space):
STOPPROCS='storeBackup wget'

echo Logfile=$LOGFILE

unset known_host

_ping_last_host() {
  # try to ping the last known active host
  # return 0 on success, otherwise 1
  if [ $known_host ]; then
    echo -n "`date` - pinging last host $known_host - " >> $LOGFILE
    ping -c 1 -s 8 -t 1 $known_host >/dev/null;
    if [ $? -eq 0 ]; then 
      # Jepp! We're done
      return 0
    else
      echo "fail" >> $LOGFILE
      unset known_host
      return 1
    fi;
  else
    return 1
  fi
  
}

_ping_range() {
  # Ping an IP-range and look for a responding host.
  # If there is one store it's IP in $known_host and return 0
  # return 0 on success, otherwise 1
  cnt=0
  echo -n "`date` - pinging range...  " >> $LOGFILE
  for i in {20..200}; # Set the range   192.168.178.20 through .200
  do
    # Ignore my own ip
    if ! [ ${class}.${i} = $myIP ]; then
      ping -c 1 -s 8 -t 1 ${class}.${i} >/dev/null

      if [ $? -eq 0 ]; then 
        echo -n "${class}.${i} - " >> $LOGFILE
        known_host=${class}.${i}
        return 0;
      fi
    fi
  done
  return 1
}

_shutdown() {  
  # Do a shutdown if we failed for $failcount rounds
  # We need a script suspend.sh in the current directory
  if [ $failcount -eq $maxfail ];then 
    echo -n "`date` - " >> $LOGFILE
    # off cause you cold place a "powersave -u" here too
    ./suspend.sh >> $LOGFILE
    echo "`date` - back from suspend" >> $LOGFILE
    failcount=0;
  fi
}

while [ 1 ];
do
  proc_found=0
  # look if uniterruptable jobs are running
  for proc in $STOPPROCS
  do
    if [ "`pgrep $proc`" != "" ];then 
      echo "`date` - $proc is running." >> $LOGFILE
      proc_found=1
      break
    fi
  done 
  echo procfound=$proc_found 

  if [ $proc_found -eq 0 ]; then
    # look for other hosts, that are alive in our subnet
    _ping_last_host
    if [ $? -ne 0 ];then
      _ping_range
      if [ $? -ne 0 ];then
        let failcount++;
        echo "failure No. $failcount" >> $LOGFILE
        _shutdown;
      else
        echo "good." >> $LOGFILE
        failcount=0;
      fi
      else
        echo "good." >> $LOGFILE
        failcount=0;
    fi
  fi
  sleep $SLEEP;
done
 
Old 07-24-2009, 02:53 PM   #9
geesh
Member
 
Registered: Feb 2004
Posts: 52

Rep: Reputation: 16
what would you consider an interruptible task? Is it possible to look for a specific task?
 
Old 07-24-2009, 05:56 PM   #10
anutosho
LQ Newbie
 
Registered: Nov 2007
Posts: 28

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by geesh View Post
what would you consider an interruptible task? Is it possible to look for a specific task?
I meant UNinterruptable task off cause.

I would consider an uninterruptable task something like a backup or getting a file (in my case a radio recording)
As usual in Linux there are several ways to look for a process running.
In my case I used pgrep, which is a combined ps and grep the Names of the programmes considered uninterruptable are stored in $STOPPROCS. This list is processed by "for proc in $STOPPROCS...".
 
Old 12-30-2018, 12:58 PM   #11
tassir
LQ Newbie
 
Registered: Dec 2018
Posts: 3

Rep: Reputation: Disabled
You can try sspender https://github.com/mountassir/sspender

It allows you to suspend your machine based on pre-defined CPU/Disk usage, and makes sure the machine wakes up at certain times when you need it to be ON.
 
  


Reply


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
ACPI & auto shutdown, suspend stevenhasty Slackware 1 12-16-2011 02:51 AM
Suspend to ram on a home server em21701 Slackware 2 06-11-2009 01:25 PM
Auto Hibernate/Suspend works, but computer restarts when resuming wild_oscar Linux - Software 2 02-02-2008 08:25 AM
auto.home assistance ejkeebler Linux - Networking 2 11-07-2005 05:09 PM
Auto suspend Donny Linux - Newbie 3 10-13-2002 04:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:30 AM.

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