Ubuntu – BASH Scripting, su to www-data for single command

bashsusvnUbuntu

I am working on automating the creation of subversion repositories and associated websites as described in this blog post I wrote. I am running into issues right around the part where I su to the www-data user to run the following command:

svnadmin create /svn/repository

There is a check at the beginning of the script that makes sure it is running as root or sudo, and everything after that one command needs to be run as root. Is there a good way to run that one command as www-data and then switch back to root to finish up?

Best Answer

Just use su - www-data -c 'svnadmin create /svn/repository' in your script run by root. So that only this command is run by www-data user.


Update for future viewers:

In case you receive a "This account is currently not available" error, consider using:

su - www-data -s /bin/bash -c 'svnadmin create /svn/repository'

( @Petr 's valuable mention about -s flag to accommodate www-data user's no login policy )