How To Avoid Sudden Outburst Of Backup Shell Script / Program Disk I/O

by on June 3, 2008 · 12 comments· LAST UPDATED August 27, 2014

in , ,

A sudden outburst of violent disk I/O activity can bring down your email or web server. Usually, a web / mysql or mail server serving millions and millions pages per months are prone to this kind of problem. Backup activity can increase current system load. To avoid this kind of sudden outburst problem, run your script with scheduling class and priority. Linux comes with various utilities to manage this kind of madness.

CFQ scheduler

You need Linux kernels 2.6.13+ with the CFQ IO scheduler. CFQ (Completely Fair Queuing) is an I/O scheduler for the Linux kernel, which is default in 2.6.18+ kernel. RHEL 4/ 5 and SuSE Linux has all scheduler built into kernel so no need to rebuild your kernel. To find out your scheduler name, enter:
# for d in /sys/block/sd[a-z]/queue/scheduler; do echo "$d => $(cat $d)" ; done
Sample output for each disk:

/sys/block/sda/queue/scheduler => noop anticipatory deadline [cfq]
/sys/block/sdb/queue/scheduler => noop anticipatory deadline [cfq]
/sys/block/sdc/queue/scheduler => noop anticipatory deadline [cfq] 

CFQ is default and recommended for good performance.

Old good nice program

You can run a program with modified scheduling priority using nice command (19 = least favorable):
# nice -n19 /path/to/backup.sh
Sample cronjob:
@midnight /bin/nice -n19 /path/to/backup.sh

ionice utility

ionice command provide more control as compare to nice command. This program sets the io scheduling class and priority for a program or script. It supports following three scheduling classes (quoting from the man page):

  • Idle : A program running with idle io priority will only get disk time when no other program has asked for disk io for a defined grace period. The impact of idle io processes on normal system activity should be zero. This scheduling class does not take a priority argument.
  • Best effort : This is the default scheduling class for any process that hasn’t asked for a specific io priority. Programs inherit the CPU nice setting for io priorities. This class takes a priority argument from 0-7, with lower number being higher priority. Programs running at the same best effort priority are served in a round-robin fashion. This is usually recommended for most application.
  • Real time : The RT scheduling class is given first access to the disk, regardless of what else is going on in the system. Thus the RT class needs to be used with some care, as it can starve other processes. As with the best effort class, 8 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window. This is should be avoided for all heavily loaded system.

The syntax is:

 
ionice options  PID
ionice options -p PID
ionice -c1 -n0  PID
 

How do I use ionice command?

Linux refers the scheduling class using following number system and priorities:

Scheduling classNumberPossible priority
real time18 priority levels are defined denoting how big a time slice a given process will receive on each scheduling window
best-effort20-7, with lower number being higher priority
idle3Nil ( does not take a priority argument)

To display the class and priority of the running process, enter:
# ionice -p {PID}
# ionice -p 1

Sample output:

none: prio 0

Dump full web server disk / mysql backup using best effort scheduling (2) and 7 priority:
# /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full
Open another terminal and watch disk I/O network stats using atop or top or your favorite monitoring tool:
# atop
Sample cronjob:
@weekly /usr/bin/ionice -c2 -n7 /root/scripts/nas.backup.full >/dev/null 2>&1
You can set process with PID 1004 as an idle io process, enter:
# ionice -c3 -p 1004
Runs rsync.sh script as a best-effort program with highest priority, enter:
# ionice -c2 -n0 /path/to/rsync.sh
Finally, you can combine both nice and ionice together:
# nice -n 19 ionice -c2 -n7 /path/to/shell.script
Related: chrt command to set / manipulate real time attributes of a Linux process and taskset command to retrieve or set a processes's CPU affinity.

Other suggestion to improve disk I/O

  1. Use hardware RAID controller.
  2. Use fast SCSI / SA-SCSI / SAS 15k speed disk.
  3. Use fast SSD based storage (costly option).
  4. Use slave / passive server to backup MySQL
Recommended readings:
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 12 comments… read them below or add one }

1 Adam Ward June 3, 2008 at 7:40 pm

Utilizing a CDP block-level backup solution will reduce your backup load tremendously.

Adam

Reply

2 Gregor June 4, 2008 at 3:59 pm

Why in heck didn’t I know about ionice? Many of my apps every day involve major disk IO which slows down everybody else. This is a life saver!

Reply

3 Steve July 7, 2008 at 9:46 pm

ionice is letting me run backup scripts and the yum updater, etc with the websites flying along instead of timing out, this is fantastic, thanks for pointing it out.

Reply

4 Amr El-Sharnoby May 23, 2009 at 1:15 pm

Thanks,
Please fix the spelling mistake; iosnice should be ionice

Reply

5 Martin August 5, 2009 at 6:56 pm

OT but have to mention that here shortly:

Often I google/yahoo for a specific problem and in the end I’m landing here, could even start looking this site.

Great work, man! Go on like that.

Greetings from Europe!
Martin

Reply

6 AskApache August 22, 2009 at 4:40 pm

Been googling for an hour, this is the finest ionice page I’ve found yet! This is exciting, saves me from having to do an unneccessary upgrade.

Reply

7 Rob November 2, 2009 at 9:14 pm

Tnx!
Allways bugging me when running cp script or another script eating up the performance.. put it in my webserver… exactly what i was looking for…and yeah also for me the same, was searching for an hour to find till ended up here and found what i want.. Tnx

Reply

8 Holger March 31, 2011 at 7:24 pm

Thanks for your hint. I wasn’t aware of the existence of ionice. Using ionice plus nice for rsync backup jobs tremendously lowers the load of your system. This is great when backing up your Linux workstation to an external connected USB hdd an still want to play a game or browse the net.

CFQ itself is not always the answer for bad responsiveness of x64 Linux hosts.

BTW: A hardware raid controller does not always help in the first place. Our IBM servers which run a KVM-based virtualisation solution bog down if you copy the HDD image of one VM to another VMs HDD image (= cloning the VM). I think ionice will help here a lot.

Reply

9 David July 12, 2011 at 10:50 am

This is stupid. A heavy IO activity shouldn’t make the system unresponsive. This behavior should be automatic. That what schedulers are for.

Reply

10 Andres July 21, 2012 at 7:43 am

Thanks for that info.

Shouldn’t you add “use SSD storage” under your suggestions to improve I/O?. I cannot understand why it’s not already on the article.

Reply

11 Julio July 23, 2012 at 6:40 pm

And what about using nice to modify priority to a Cron Job that consists on a “/bin/php -f /home/user/script.php” ?
Is it possible? I tried creating a bash script that executes php, but it’s not executed.

Reply

12 Paul Thomson September 30, 2013 at 9:55 am

Perfect for s3cmd sync not hoggin’ resources.

Keep up the good work, stay safe!
Paul :-)

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: