Scheduling in Linux: Run a Task When Computer Is Idle (= No User Input)

Scheduling in Linux: run a task when computer is idle (= no user input)

When the machine in question is a desktop, you could hook a start/stop script into the screensaver so that the process is stopped when the screensaver is inactive and vice versa.

Automatically run a command or program when I'm away from keyboard?

On linux just use crontab. For Ubuntu it's explained here, but it will most likely apply to other linux distros too.

It's a bit more difficult under windows but this article describes it well.

run cron job only when machine is idle (linux)

This is a typical case of TOCTOU - you check if the system is idle, and start your process - but after your check, as or before your process starts, something else caused another process in the system to kick off and you still load the system more than necessary.

The "correct" way to do this is to give your process a low priority, using the nice command. By the way, your loop to check cpu usage will use 100% cpu, so it probably wouldn't work unless THE FIRST time you check it's idle.

You already have a "proc_nice(10)", so that should do the job. I don't see any point in spending effort in identifying whether the system is busy or not.

If you watned to, at suitable points in your code, you could do something like:

 if (check_cpu_usage() > 50%) sleep(1second); 

But I'm not sure if that's much use - if the system is busy, a "nice" process won't receive much CPU-time, and thus won't compete with other processes that run at higher priority.

How to find repeating sequence of characters in a given array?

For your examples, my first approach would be to

  1. get the first character of the array (for your last example, that would be C)
  2. get the index of the next appearance of that character in the array (e.g. 9)
  3. if it is found, search for the next appearance of the substring between the two appearances of the character (in this case CARPENTER)
  4. if it is found, you're done (and the result is this substring).

Of course, this works only for a very limited subset of possible arrays, where the same word is repeated over and over again, starting from the beginning, without stray characters in between, and its first character is not repeated within the word. But all your examples fall into this category - and I prefer the simplest solution which could possibly work :-)

If the repeated word contains the first character multiple times (e.g. CACTUS), the algorithm can be extended to look for subsequent occurrences of that character too, not only the first one (so that it finds the whole repeated word, not only a substring of it).

Note that this extended algorithm would give a different result for your second example, namely RONRON instead of RON.



Related Topics



Leave a reply



Submit