I want to put my Debian Wheezy home server to sleep, if it wasn't accessed through the network for a certain amount of time.

I thought of a bash script that uses netstat to count the number of active connections, sleeps for some seconds, runs netstat again, and so on.

Is there any better way to achieve this without using sleeping every second? Is there any way to react to the event 'incoming network connection' and reset some kind of countdown timer?


Idle network detection is generally handled with a shell script, as you've done. A simple web search for 'linux sleep network idle' indicates at least 3 easy to find scripts for this problem.

Whether you copy an existing script or write your own, there's no reason to run it every second. You can easily create a cron job to run it every 30 seconds. Most users wouldn't want the server to shut down before it's been idle for 5-15 minutes, so per second precision is not required. Some users only run the script once every 20 minutes, which would sleep the machine 30 minutes after it becomes idle, on average.

  • my problem is that running the script every 30 seconds will not detect short connections that lasted for only a few seconds. I do only want to shut down if really no incoming traffic happened. – muffel Mar 21 '14 at 0:08
  • I see your problem. Most scripts look at if total bandwidth numbers, so running infrequently isn't a problem. I suggest you do the same. Search for the terms I listed above to see examples. – Freen Mar 21 '14 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.