Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

sudo

svn

I have a SVN repository server that runs under the repository user. I want to run a script after every post-commit action. I wrote a shell script that runs from the hook after every commit. It needs to be run as root. This is why I used sudo in the script, but it didn't work. Is there any way to run the script as root?

sudo su
echo "password"
svn export --force file:///home/repository/trunk/ /home/memarexweb/public_html/devel/
chmod -R 777 /home/memarexweb/public_html/devel/
like image 534
Loqman Avatar asked Sep 21 '11 11:09

Loqman


1 Answers

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.)

like image 138
Loqman Avatar answered Oct 07 '22 06:10

Loqman