Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
        ;;
    *noop*)
        echo 'noop' > /sys/block/sda/queue/scheduler
        ;;
    *)
        exit 0
esac

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

See also http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

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 ~/1.sh and executing gives $ ./1.sh ./1.sh: line 3: syntax error near unexpected token "$SCHEDULER"' ./1.sh: line 3: case in "$SCHEDULER";' then i put line #!/bin/sh -e from rc.local and executed $ ./1.sh ./1.sh: 4: Syntax error: word unexpected (expecting "in") –  Kangarooo Jan 19 '12 at 16:56
    
$ bash 1.sh 1.sh: line 3: syntax error near unexpected token "$SCHEDULER"' 1.sh: line 3: case in "$SCHEDULER";' –  Kangarooo Jan 19 '12 at 17:57
1  
See update (remove the ;) –  bodhi.zazen Jan 19 '12 at 18:03
show 1 more comment

Your Answer

 
discard

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.