How can I switch to the user jenkins in the middle of a ssh script?
Asked Answered
C

3

6

I run

ssh root@myhost "sh -x" < myremotecommands.sh

where myremotecommands.sh contains:

#!/bin/sh
sudo su
apt-get update
sudo su -l -p jenkins
whoami

however the command whoami returns 'root'. I need to be user jenkins to perform some installations.

How can I switch to the user jenkins in the middle of the script ?

Crud answered 7/8, 2011 at 22:43 Comment(0)
R
2

Use $USER. That will give you the username you logged in as. Whoami returns the user you're currently operating as.

Rhomb answered 7/8, 2011 at 22:49 Comment(1)
Use $USER. Why?Jephum
R
10

You just have to use "su" command with "-s /bin/bash" argument. It´s needed because jenkins user was not supposed to be used interactively, so it doesn´t have the bash defined.

su jenkins -s /bin/bash

After this, the "whoami" command will report you as "jenkins" user.

Rexfourd answered 11/3, 2014 at 22:46 Comment(0)
R
2

Use $USER. That will give you the username you logged in as. Whoami returns the user you're currently operating as.

Rhomb answered 7/8, 2011 at 22:49 Comment(1)
Use $USER. Why?Jephum
C
1

Problem solved:

#!/bin/sh
sudo su
apt-get update
su jenkins <<HERE
whoami
echo usr=$USER
HERE

will output:
jenkins
usr=root

Source: http://www.daniweb.com/software-development/shell-scripting/threads/14498

Crud answered 8/8, 2011 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.