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