2014-09-02 305 views
0

我有一个脚本,它复制一个文件,然后在多个系统上解压并安装它(agent-service)(IP从systems.txt文件中读取)。在脚本中,我想以用户“test”的身份启动代理服务。但是,在脚本执行后,当我检查目标系统时,代理服务显示为以“root”用户身份运行。这里有什么可能是错的?我没有在脚本中使用su命令吗?su命令在shell脚本

~]# 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 [email protected]$host:/opt 


ssh -n [email protected]$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

即读为:切换到用户测试,这无关,饰面,和然后以原始用户的身份运行该开始。如果你把它保存在一行中,它可能会工作('su -c the-command')。顺便说一句:有些东西像'start-stop-daemon',它可以处理很多你可能觉得有用的东西。 – Wrikken 2014-09-02 09:13:35

回答

4

使用su,你在这里做的是派生无关从而做一个新的外壳立即退出。

要么传递命令到su

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

或者以类似的方式使用sudo

sudo -u test /opt/linux/agent-service start 
+0

非常感谢,帮助。更新脚本如下,它的工作。 su -c“/ opt/linux/agent-service start”测试 – user3331975 2014-09-02 10:37:56