0

When I run a script such as this:

ssh -t root@10.10.10.10 '/tmp/somescript.sh'

where the script is defined as:

#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
su myuser -  # <------- NOTICE THIS ! ! ! ! 
rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm

Notice the above su command.

If I comment-out the su command, the script runs remotely and then my shell prompt returns to where I came from ( same server where I ran the ssh command above )

But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.

How can I prevent that ? Making sure that the issuer of the rpm command is a different user than root just a listed ?

gextra
  • 205
  • 2
  • 6

2 Answers2

4

Run the rpm command with sudo:

#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
sudo -u myuser rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm
Chris Montanaro
  • 830
  • 1
  • 7
  • 8
1

su starts a shell. When you exit that shell, then the rpm command will execute. If you're changing user to execute the rpm command, then

su -c 'rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm'  myuser
glenn jackman
  • 4,630
  • 1
  • 17
  • 20