How to find out that a running Linux machine is idle or not
I have my laptop, most of the time connected to internet, the speed of internet is quite slow. When i download some big files, then i am not able to surf web sites because of slow speed of internet. My plan is to write a bash script and run it in cron jobs, when it finds the system is idle then it starts a process(the process which will dow开发者_Python百科nload the big files preferably wget) automatically and when i use the laptop then the process is killed. Similar to a screensaver( I have found some ways at Scheduling in Linux: run a task when computer is idle (= no user input) but i dont use screensaver on my machine neither i want to depend on gnome application like xscreensaver ). I use Ubuntu linux, is there a way to find proper idle time of a Linux mahine.
idle = no keyboard, touch pad or mouse clicks.
Is there some way to find network activity in such way, like if their is no internet traffic then my sript start executing another wget script and if my browser request for web pages then it will stop executing the script.
please suggest me a proper way.. should i implement it using idle time, it will be more effective if i use network traffic into consideration.
There is one way but it's pretty hacky. You could listen to the appropriate devices (in the /dev area).
For example, on my current machine:
sudo cat /dev/input/by-path/platform-i8042-serio-0-event-kbd
Generates a whole load of bytes if I'm typing and blocks (outputs nothing) if I'm not. Something similar should work for the mouse but it doesn't seem to on my system.
By default the permissions for the input devices are pretty strict, you may want to chgrp them (for example). I would definitely not leave something like that running as root! Be aware there are lots of possible security implications of messing about with the permissions in dev. I would definately not consider this if it's a shared box.
This is not a simple program to write (you may be able to do it as a bunch of scripts signaling each other but I personally wouldn't) and using something like xscreensaver (or even just CPU usage) would be simpler (but perhaps less fun).
I'd recommend a much simpler method: Just tell wget how much bandwith it may eat up, to leave some free for surfing the web.
Just use:
wget --limit-rate=1.5k http://example.com
See https://unix.stackexchange.com/a/122816/65781
I put those codes into a Github repo for better issue tracking: https://github.com/ceremcem/on-idle
精彩评论