0

I have a script which copies a file, then untar and install it (agent-service) on multiple systems (IPs are read from systems.txt file). In the script, I wanted to start the agent-service as user "test". However after the script execution, when I check the target system, the agent-service is shown as running as "root" user. What could be wrong here? Am I not using su command correct within the script?

~]# ps -ef | grep agent-service

    root     23511 15196  0 02:12 pts/3    00:00:00 agent-service

Script>

#!/bin/bash
export AGENT=linux-5.8.1.tar.gz

while read host; do

scp $AGENT root@$host:/opt


ssh -n root@$host 'cd /opt/linux;
tar zxvf linux-5.8.1.tar.gz;
mkdir /opt/hyperic;
useradd -m test;
chown -R test:test /opt/linux;
su - test;
/opt/linux/agent-service start'

done < systems.txt
1
  • 1
    That reads as: switch to user test, which has nothing to do, finishes, and then run that start as the original user. If you keep it in one line it might work (su -c the-command). BTW: there are things like start-stop-daemon which take care of a lot of things you might find useful. Commented Sep 2, 2014 at 9:13

1 Answer 1

5

Using su as you do here spawns a new shell that has nothing to do thus exits immediately.

Either pass the command to su:

su - test -c /opt/linux/agent-service start

Or use sudo in a similar manner:

sudo -u test /opt/linux/agent-service start
Sign up to request clarification or add additional context in comments.

1 Comment

thanks much, that helped. updated the script as below and it worked. su -c "/opt/linux/agent-service start" test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.