Skip to content
March 18, 2013 / gus3

Tired of su-ing

One axiom of computing is to script commonly-used command sequences, akin to C’s functions, Pascal’s procedures, and Ada’s subprograms. But what if the command sequence involves “su” for elevated privileges? I found an idiom that Works For Me.

This example will pull pages from the swap space back into RAM:

#!/bin/sh

case `id -u` in
    # The task is accomplished in the 0 case,
    # where we know we have root privileges.
    0) /sbin/swapoff -a
       /sbin/swapon -a
    ;;
    # Everyone else gets to provide a password:
    *) exec /bin/su -c "`which $0`" - ;;
esac

Anything requiring elevated privileges, or even simply someone else’s privileges, can be scripted this way. I have the above script in ~/bin/unswap. I have three other scripts in the same directory, called “slow”, “normal”, and “fast”, which set the CPU frequency governor to powersave, ondemand, or performance.

The one caveat is to hard-code the paths to the root-executed programs (which you already do in scripts, right?). You don’t want to execute a non-standard “swapon” that isn’t where you expect it to be.

(If someone would like to contribute an example using “sudo” instead of “su” it would be greatly appreciated.)

About these ads

5 Comments

Leave a Comment
  1. eMBee / Mar 18 2013 9:32 pm

    interesting, but running su -c "swapon -a" as root makes su not ask for a password, at worst it will execute another shell so you could just use that in your script and it should do the right thing either way.

    greetings, eMBee.

    • gus3 / Mar 19 2013 6:45 pm

      And hope you get your quoting right, if need be. That isn’t a concern with my script.

      • eMBee / Mar 22 2013 3:57 am

        i don’t see how quoting factors into this. you’s still have a scripts just like yours, only that the content can be reduced to one line without the test…

        greetings, eMBee.

        • gus3 / Mar 22 2013 5:08 pm

          In the example you gave, that’s true. However, if you want to script something with quotes, you increase the chance of error by wrapping your script in more quotes. Do you use single quotes, or double quotes, or back quotes, do you need to escape double quotes inside single quotes, etc etc etc… Readability suffers as “leaning toothpicks” take over; Perl is particularly guilty of this. My example above wraps a script “as-is” while seeking root privileges as desired.

  2. Bob Robertson / Mar 19 2013 10:40 am

    Cool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: