Thursday, October 20, 2011

Using Fabric with sudo

If you've ever had to use Fabric, one of the issues is that your scripts must return an error code of 0 in order for the sudo() command to assert that the command executed successfully. Any non-zero error code will result in an error message.

Fatal error: sudo() encountered an error (return code 1) while executing 'sudo"...

If you're using bash scripts, this means that any "set -e" or "bash -e" statements that trigger an abnormal exit. The "kill -0 ", which allows you to test whether a process exists and can be killed, suffers from a flaw in that if you provide a PID that does not exist, it will trigger an exception and cause bash to break out if "set -e" or "bash -e" is set (normally you can use $? to check the return value).

You also should check the integer value (if [ $? -eq 0 ]; then or if [ ?! -eq 1]; then) to determine which step to use.


  1. That's holds true for "run" too. And you can ignore errors anyway - run with "fab -w" or use "with settings(warn_only=True):" in the functions

  2. I found your own this unique write-up whilst looking for a number of linked home elevators range seek... The particular an excellent write-up.. maintain publishing besides up-date the info.
    Regards: chiffon