0

I'm working with the edx open source code base and I'm putting together a dotfile to make it easier to perform various tasks on the server. I'm having trouble with the following bash function

edx-compile_assets() { 
    sudo -H -u edxapp bash
    source /edx/app/edxapp/edxapp_env
    cd /edx/app/edxapp/edx-platform
    paver update_assets cms --settings=aws
    paver update_assets lms --settings=aws
}

When sudo -H -u edxapp bash is run, the function halts and nothing happens, when I exit from that env, then the remaining functions execute not as the edxapp user, but as a normal user, which causes the commands to fail.

So basically, it looks like sudo -H -u edxapp bash starts a separate process, and when that process ends, the remainder of the function executes I guess what I'm looking for is a simple way to run commands as the edxapp user which is activated with sudo -H -u edxapp bash

Any help is appreciated, thanks :)

SamAko
  • 3,485
  • 7
  • 41
  • 77
  • Does `edxapp_env` need to be run in the process that calls `edx-compile_assets`, or can it run in the subprocess started by `sudo`? – chepner Aug 13 '15 at 17:15
  • yes, edxapp_env should run in the subprocess as well – SamAko Aug 13 '15 at 17:38
  • 2
    `edx-compile_assets` should probably be a script that you run *with* `sudo`, rather than a function that *calls* `sudo`. – chepner Aug 13 '15 at 17:46
  • Also note that you can call single commands as another user with `su edxapp -c some-command`; but @chepner has the right idea. – Wolf Aug 13 '15 at 17:48
  • Or you could feed the commands on standard input (in a here document, for instance) to `sudo -H -u edxapp bash` – tripleee Aug 13 '15 at 18:00

1 Answers1

0

IIRC, bash takes a-c option, allowing you to invoke bash and immediately execute a command/script. So I'd put the extra statements (source, cd, paver ...) into a script and add -c myRemainingScript.sh at the end of your sudo line.

donjuedo
  • 2,475
  • 18
  • 28
  • ok, I'll give this a shot. was hoping I wouldn't have to create extra files though, because I've got a bunch of such functions, and creating files to hold commands will be a bit hard – SamAko Aug 13 '15 at 17:43
  • How about stringing the commands together with semicolons, like `... bash -c "source dirPath ; cd filePath ; paver xxx ; paver yyy " ? – donjuedo Aug 13 '15 at 17:47