How do I run a shell script as root (sudo)?

I was searching around and found this useful solution:

Edit your sudoers file to allow running certain commands without a password.

It's best to split your post-commit script into two parts, one of which will be run through sudo.

  • entry in /etc/sudoers:

    loqman    ALL=(root) NOPASSWD: /usr/local/bin/svn-postcommit-export
    
  • Your post-commit hook:

    #!/bin/sh
    sudo /usr/local/bin/svn-postcommit-export
    
  • Script /usr/local/bin/svn-postcommit-export:

    #!/bin/sh
    svn export --force file:///home/repository/trunk/ /home/memarexweb/public_html/devel/
    chmod -R 777 /home/memarexweb/public_html/devel/
    

    (You can choose any name and put the script anywhere; I just suggested svn-postcommit-export as an example, and /usr/local/bin as a common location.)


sudo su

starts a new process, owned by the root user. After that process is terminated or stopped, the next line is executed, again as the user that executes the script.

A possible solution is to run the whole script using sudo, and to give that use sudo rights to exectute the scripts. In order to do that, you need to edit the /etc/sudoers file using the visudo command.

Tags:

Svn

Shell

Sudo