When your computer is shut down (or the cron daemon is otherwise not running), cron jobs will not be started.
If you have jobs that you would like to run after the fact during those times when the computer is shut down, use anacron. Installed by default, see "man anacron", "man anacrontab", or the file /etc/anacrontab for more info.
Ubuntu uses anacron by default for crontab entries in:
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly
leaving the remaining crontabs to be handled by the main cron daemon, specifically:
/etc/crontab
/etc/cron.d
/var/spool/cron
NOTES
Anacron itself does not run as a daemon, but relies on system startup scripts and cron itself to run.
On the Ubuntu 8.04 box I'm looking at, /etc/init.d/anacron is run at boot, and again by cron each morning at 07:30.
The README at /usr/share/doc/anacron/README.gz has a slight bit more info than is contained in the manpages.
EXAMPLES
For simple "daily", "weekly", "monthly" jobs, put a copy of or a symlink to the script in one of the /etc/cron.{daily|weekly|monthly} directories above. Anacron will take care of running it daily/weekly/monthly, and if your computer is off on the day the "weekly" scripts would normally run, it'll run them the next time the computer is on.
As another example, assuming you have a script here: /usr/local/sbin/maint.sh
And you wish to run it every three days, the standard entry in /etc/crontab would look like this:
# m h dom mon dow user command
0 0 */3 * * root /usr/local/sbin/maint.sh
If your computer was not on at 00:00 on the 3rd of the month, the job would not run until the 6th.
To have the job instead run on the 4th when the computer is off and "misses" the run on the 3rd, you'd use this in /etc/anacrontab (don't forget to remove the line from /etc/crontab):
# period delay job-identifier command
3 5 maint-job /usr/local/sbin/maint.sh
The "delay" of "5" above means that anacron will wait for 5 minutes before it runs this job. The idea is to prevent anacron from firing things off immediately at boot time.
i found a perl script here http://www.skolnick.org/cgi-bin/list.pl?file=serverload.pl
This should do what you need.
You could probably implement this with a simple shell script that parse uptime, and
only being execution once the load average has lowered to a certain value.
Beware that if you server is constantly busy, with a run away process, then your
cronjobs will never execute!.
Perhaps a better idea if you have memory is to run you cron jobs at a the lowest OS
priority, thus they will only consume spare resources.
Best Answer
There are caveats to pay attention to. Since the question doesn't specify an exact OS (but implies it is some Unix like OS), the list of caveats will depend on specific OS and version. The most important to keep in mind are:
nice
is intended to affect how much CPU time is given to a process, but not how much RAM or I/O capacity. Thus instead of the intended effect other possible outcomes include:nice
has no effect at all, because the backup process was I/O bound to begin with, and the I/O scheduling is unaffected bynice
. If the OS happens to be a recent Linux version, the I/O scheduling may or may not be affected bynice
depending on whichionice
setting is being used.Moreover even the exact effect on CPU scheduling depend a lot on the specific operating system and settings. Some kernels have settings which will allow a process to run at a higher or lower priority than those reachable by using the
nice
command.One caveat that I have run into myself appears to be specific to Ubuntu 14.04. In the default configuration it groups processes for scheduling purposes. Each group then receives a fair share of CPU time.
nice
only affects how CPU time is allocated to processes within such a group, but not how much is allocated to each group. For me that completely undermined the use ofnice
, because a low priority process could still take away CPU time from processes in different groups.