Cron – Using nice scheduling priority with tar/gzip cron script

cronnice

I do large backups that put huge strains on my server when using tar/gzip. I've got the task setup as a cronjob which accesses my script that handles the backup. I know that nice might be able to possibly help in this situation, but I'm a bit uncertain of the proper way to use it.

I've got the following commands within my script:

tar -cf 
gzip -9 

Would I just add the nice command in front of it like so to reduce the priority?:

nice -n 13 tar -cf 
nice -n 13 gzip -9

Are there any caveats to using this approach? thanks.

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:

  • The backup takes longer time to complete due to being given less CPU time. But it will use just as much RAM as it used to and now it will use that RAM for longer time. The system is slowed down due to having less RAM for other purposes, and this slowness will last longer time than it used to.
  • The use of nice has no effect at all, because the backup process was I/O bound to begin with, and the I/O scheduling is unaffected by nice. If the OS happens to be a recent Linux version, the I/O scheduling may or may not be affected by nice depending on which ionice 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 of nice, because a low priority process could still take away CPU time from processes in different groups.