簡體   English   中英

shell腳本中的su命令

[英]su command in shell script

我有一個腳本,該腳本可以復制文件,然后解壓縮並將其安裝(代理服務)到多個系統上(IP是從systems.txt文件中讀取的)。 在腳本中,我想以用戶“ test”的身份啟動代理服務。 但是,在腳本執行后,當我檢查目標系統時,代理服務顯示為以“ root”用戶身份運行。 這有什么問題嗎? 我在腳本中使用的su命令是否正確?

~]# 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

像在此處一樣使用su會生成一個無用的新shell,因此會立即退出。

將命令傳遞給su

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

或以類似方式使用sudo

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM