Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out that a running Linux machine is idle or not

Tags:

linux

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. 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.

like image 772
Shiv Deepak Avatar asked Aug 18 '10 13:08

Shiv Deepak


People also ask

How do I check what processes are running in Linux?

The ps command displays your currently running processes in real-time. To test this, just open your terminal and run the ps command like so: This will display the process for the current shell with four columns: CMD returns the name of the command that launched the process.

How do I know if my system is running at idle?

A normal, idle system should be mostly zeros across the board, sometimes with a few small bursts while data is being written, as in the screenshot below. If, however, you run a disk-intensive utility like find, you’ll see its name and throughput listed clearly in iotop.

How to check the current runlevel on Linux?

In this guide, we’ll show you how to check the current runlevel on Linux. Privileged access to your Linux system as root or via the sudo command. The following command can be used to view the current runlevel. This will only work on some systemd distros, but should work for any Sys-V system.

How do I find the process ID of a running process?

To find the process ID of a running process, you can use the pgrep command followed by the name of the process like so: To kill the iTerm2 process in the screenshot above, we will use any of the commands below.


1 Answers

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).

like image 139
HNJSlater Avatar answered Oct 02 '22 05:10

HNJSlater