Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
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.
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
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.
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.
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.
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.
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
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...".
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.