Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When running

cat /sys/block/sda/queue/scheduler

an example answer is the line

noop anticipatory deadline [cfq]

I want to write a script such that if the cat line answer contains

  1. deadline then do X; or
  2. If deadline doesn't appear, but it contains noop then do Y

So, how can I write a script that:

  1. Checks the cat answer

  2. In case of deadline does:

    sudo sed -i 's/^exit 0$/\necho deadline > \/sys\/block\/sda\/queue\/scheduler\necho 1 > \/sys\/block\/sda\/queue\/iosched\/fifo_batch\nexit 0/' /etc/rc.local
  3. If deadline doesn't exist but 'noop' does then do:

    sudo sed -i 's/^exit 0$/\necho noop > \/sys\/block\/sda\/queue\/scheduler\nexit 0/' /etc/rc.local
  4. Else nothing.

share|improve this question
up vote 2 down vote accepted

Add to rc.local, above exit 0

SCHEDULER=$(cat /sys/block/sda/queue/scheduler)

case "$SCHEDULER" in
    *deadline* )
        echo 'deadline' > /sys/block/sda/queue/scheduler
        echo '1' > /sys/block/sda/queue/iosched/fifo_batch
        echo 'noop' > /sys/block/sda/queue/scheduler
        exit 0

Run the script with sudo, or if it is an init script, it will run as root.

See also

share|improve this answer
CLI in one line. maybe its somehow possible to translate this to CLI? in link i see theres mentioned awk i think somwhere saw it with something simmilar what could make it work. – Kangarooo Dec 18 '11 at 22:56
bash, awk, python, you could probably script it in a number of ways. Either write a proper upstart script of make you life easier. In /etc/rc.local, just use an echo for each case, no real need to re-write rc.local with sed. See my updated script - much easier to read and understand. – bodhi.zazen Dec 18 '11 at 23:05
So i put that in /etc/rc.local up before exit 0 ? I tryd this all in file ~/ and executing gives $ ./ ./ line 3: syntax error near unexpected token "$SCHEDULER"' ./ line 3: case in "$SCHEDULER";' then i put line #!/bin/sh -e from rc.local and executed $ ./ ./ 4: Syntax error: word unexpected (expecting "in") – Kangarooo Jan 19 '12 at 16:56
$ bash line 3: syntax error near unexpected token "$SCHEDULER"' line 3: case in "$SCHEDULER";' – Kangarooo Jan 19 '12 at 17:57
See update (remove the ;) – bodhi.zazen Jan 19 '12 at 18:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.