2014-09-02 413 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