Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I schedule a job to run only if the system is idle? E.g. I want to run some cleanup task every week, but only if it does not interfere with other users.

I know I can schedule a task to run weekly with cron, however, I see no way of having cron skip the job if the system is not idle. I am also aware that it is not clearly defined what 'idle' means. But one way of determining if a system is idle is looking at the current system load. So let's say that we consider the system as idle if current system load is below $threshold.

Another (better in some cases) way of determining if the system is idle is to check if system load is below $threshold AND no users are currently logged in.

So how to do this in Linux/Unix? (I am primarily concerned with doing this on Debian/Ubuntu based GNU/Linux systems as well as RHEL and similar redhat based systems).

share|improve this question

2 Answers 2

There's no built-in way to do this. You just have to put in a block at the top of your script that measures the load and exit()'s if it's above a predetermined value.

share|improve this answer
    
Yeah, I have thought about that. I would also like the script to be stopped if the system is suddenly no longer idle. Say, if a user logs on... This does not seem trivial to implement. –  Thomas Dec 14 '12 at 8:14

Alternatively, you can start the job anyway, but have it renice itself. That signals to the kernel that its desires for CPU are relatively unimportant, and that it should only be given slots when they system has nothing competing with it (or, in the limting case of nice +20, when nothing else wants the CPU).

I know of no comparable commands for memory and I/O "niceness", but if the system's busy and your job therefore gets hardly any run-time, its memory footprint will all end up on swap space, and it won't need any I/O bandwidth.

share|improve this answer
    
Renicing the process is not enough. I do not want it to run at all if the system is not idle. This is mostly due to the process being I/O intensive. I am aware of the ionice facility, however, it does not seem to work very well. Even at the lowest priority, the process have a non-trivial negative effect on I/O performance. –  Thomas Dec 14 '12 at 8:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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