Tag: ubuntu

Slowing down (limiting) tar, mysqldump or other processes to save IO bandwidth

Sometimes we want to perform some sort of tasks that consume whole available IO bandwidth. This may lead to some unexpected behaviours from our OS. OS might even kill the given process due to resource lack.

Lets take an example. We want to tar.gz a huge directory with a lot of files in it. Our machine also have a web-server which serves several sites. If we start "taring" our directory, it might lead to timeouts on server (it won't be able to respond as fast as we would expect). On the other hand, we don't care so much about the time needed to create archive file. We always can throw it in screen and detach it.

# Standard approach - will slow IO response time
tar -cvf ./dir.tar ./dir

pv to the rescue!

To slow things down to a tolerable level we will use pv tool. pv allows a user to see the progress of data through a pipeline, by giving information such as time elapsed, percentage completed (with progress bar), current throughput rate, total data transferred, and ETA. It can also limit the speed of incoming data, if used wisely.

To tar a file with a given speed (in MB) we need to use following command:

tar -chf - ./dir | pv -L 2m > ./dir.tar

Above example will allow us to tar ./dir with max speed 2MB/s.

We could use the same method to slow down a mysqldump method:

mysqldump --add-drop-table -u user -p password -h host db_name | bzip2 -c > ./dump.sql.bz2

Running a script on startup before X starts in Ubuntu Linux

I have two xorg.conf files that I use, depending on where I am. Lately I got sick of switching them manually like this:

sudo cp /etc/X11/xorg.conf.h /etc/X11/xorg.conf
sudo pkill X

So I decided to add switching script to rc.local file. Unfortunately rc.local seems to start after all other services. Of course this would work with sudo pkill X included. But to be honest, killing X every time I turn on my computer is a bit lame.

Run levels to the rescue

You can read about run levels here. I'll just tell you that you need to check your current run level by running this command:

runlevel

You'll get something like this:

[~]$ runlevel 
N 2
[~]$ 

My Ubuntu is by default using run level 2. So now I know that I need to hookup before X starts in this run level. To do this, you need to create a script in /etc/rc2.d/ directory. If you do a ls -al in any or RC dirs, you'll see that all the scripts there are just symlinks to /etc/init.d/ scripts. It is convenient to store all of them there, because they might be reused in other run levels.

Naming convention

All scripts (aliases) that should be executed in given run level, have a 'SNUMBER' prefix. S probably states for "Start" and number determines the order in which all the scripts well be executed (well there is also LSB that might "disrupt" the specified order but fortunately not in our case).

Example of rc2.d:

lrwxrwxrwx   1 root root    15 2012-04-25 16:42 S20mysql -> ../init.d/mysql
lrwxrwxrwx   1 root root    15 2011-06-29 09:55 S20nginx -> ../init.d/nginx
lrwxrwxrwx   1 root root    17 2011-11-14 21:25 S20postfix -> ../init.d/postfix
lrwxrwxrwx   1 root root    22 2011-06-29 02:03 S20redis-server -> ../init.d/redis-server
lrwxrwxrwx   1 root root    16 2012-04-28 21:51 S20tcpspy -> ../init.d/tcpspy
lrwxrwxrwx   1 root root    17 2011-06-22 01:18 S20vboxdrv -> ../init.d/vboxdrv
lrwxrwxrwx   1 root root    17 2011-06-21 22:40 S20winbind -> ../init.d/winbind
lrwxrwxrwx   1 root root    19 2011-06-22 00:05 S25bluetooth -> ../init.d/bluetooth
lrwxrwxrwx   1 root root    20 2011-06-22 00:05 S50pulseaudio -> ../init.d/pulseaudio
lrwxrwxrwx   1 root root    15 2011-06-22 00:05 S50rsync -> ../init.d/rsync
lrwxrwxrwx   1 root root    15 2011-06-22 00:05 S50saned -> ../init.d/saned
lrwxrwxrwx   1 root root    19 2011-06-22 00:05 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx   1 root root    14 2012-05-27 19:33 S75sudo -> ../init.d/sudo
lrwxrwxrwx   1 root root    17 2011-06-21 22:39 S91apache2 -> ../init.d/apache2
lrwxrwxrwx   1 root root    22 2011-06-22 00:05 S99acpi-support -> ../init.d/acpi-support
lrwxrwxrwx   1 root root    21 2011-06-22 00:05 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx   1 root root    18 2011-06-22 00:05 S99rc.local -> ../init.d/rc.local

X conf switchin script

I've named this script 'xselector' and I've placed it in /etc/init.d with rest of scripts. Remember to give this script execution rights!

#!/bin/sh
video_home(){
  rm /etc/X11/xorg.conf
  cp /etc/X11/xorg.conf.dom /etc/X11/xorg.conf
}

video_work(){
  rm /etc/X11/xorg.conf
  cp /etc/X11/xorg.conf.praca /etc/X11/xorg.conf
}

DAY=$(date +"%u")
HOUR=$(date +"%H")

# If this is a work day
if [ "$DAY" -lt 6 ]; then
  # And these are hours when I'm @ work
  if [ "$HOUR" -gt 7 -a "$HOUR" -lt 18 ]; then
    video_work
  else
    video_home
  fi
else
  video_home
fi

After we create our script we just need to add it like this:

sudo ln -s /etc/init.d/xselector /etc/rc2.d/S15xselector

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑