Search code examples
pythonlinuxbashquoting

failing to run command as different user


trying to install pip inside a virtual env under a non root user called jenkins

when I ssh into the server and do this: sudo su jenkins cd into relevant directory (autoenv kicks in and activates the virtual env) pip install -r requirements.txt meaning it works.

But the startup scripts runs as root...

I've tried

su - jenkins -c "echo $(whoami) ;" output : root

tried: sudo su jenkins & "echo $(whoami) ;

tried: sh my_bash.sh and my_bash.sh sudo su jenkins echo $(whoami)


Solution

  • Replace

    su - jenkins -c "echo $(whoami)"
    

    by

    su - jenkins -c 'echo $(whoami)'
    

    to stop your shell executing $(whoami) as user root before running su - jenkins echo root.