简体   繁体   English

如何找出正在运行的 Linux 机器是否空闲

[英]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 download the big files preferably wget) automatically and when i use the laptop then the process is killed.我的计划是编写一个 bash 脚本并在 cron 作业中运行它,当它发现系统空闲时,它会自动启动一个进程(该进程将下载大文件最好是 wget),当我使用笔记本电脑时,该进程是被杀。 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 ).类似于屏幕保护程序(我在 Linux中的调度中找到了一些方法:在计算机空闲时运行任务(= 没有用户输入),但我不在我的机器上使用屏幕保护程序,我也不想依赖像 xscreensaver 这样的 gnome 应用程序)。 I use Ubuntu linux, is there a way to find proper idle time of a Linux mahine.我使用 Ubuntu linux,有没有办法找到 Linux 机器的适当空闲时间。

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.有没有办法以这种方式找到网络活动,比如如果他们没有互联网流量,那么我的 sript 开始执行另一个 wget 脚本,如果我的浏览器请求网页,那么它将停止执行脚本。

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.有一种方法,但它非常hacky。 You could listen to the appropriate devices (in the /dev area).您可以收听适当的设备(在 /dev 区域中)。

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).默认情况下,输入设备的权限非常严格,您可能想要 chgrp 它们(例如)。 I would definitely not leave something like that running as root!我绝对不会让这样的东西以 root 身份运行! Be aware there are lots of possible security implications of messing about with the permissions in dev.请注意,弄乱 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).这不是一个简单的程序来编写(你可以将它作为一堆脚本来做,但我个人不会)并且使用像 xscreensaver 这样的东西(甚至只是 CPU 使用率)会更简单(但可能更少乐趣)。

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.我推荐一个更简单的方法:只需告诉wget它可能消耗多少带宽,留出一些用于上网冲浪。

Just use:只需使用:

wget --limit-rate=1.5k http://example.com

See https://unix.stackexchange.com/a/122816/65781https://unix.stackexchange.com/a/122816/65781

I put those codes into a Github repo for better issue tracking: https://github.com/ceremcem/on-idle我将这些代码放入 Github 存储库中以更好地跟踪问题: https : //github.com/ceremcem/on-idle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM