Bash 脚本 su 退出 shell

发布于 2024-12-27 20:46:55 字数 324 浏览 2 评论 0原文

我正在尝试编写一个 bash 脚本,该脚本以不同的用户身份执行多个命令来自动化安装过程。

在某一时刻,我以

su gpadmin
gpperfmon_install --enable --password password --port 5432
y  
gpstar
y 

root 身份运行 su gpadmin,一旦运行,脚本就会停止,直到我退出 gpadmin 并以 root 身份返回。

如何在不停止脚本的情况下切换到新用户?

抱歉我的无知,我尝试用谷歌搜索但没有发现任何有用的东西。 任何帮助将不胜感激。

Im trying to write a bash script that executes several commands as different users to automate an install process.

At one point I have

su gpadmin
gpperfmon_install --enable --password password --port 5432
y  
gpstar
y 

The su gpadmin is run as root, and once it is run the script stops until I exit gpadmin and return as root.

How would one switch to a new user without halting the script?

Sorry for my ignorance, I attempted to google but found nothing that worked.
Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2025-01-03 20:46:55

您需要使用 su 的 -c 选项才能运行命令然后退出。使用您为 su 提供的选项,它将运行 gpadmin 的默认 shell,该 shell 将阻塞,直到 shell 退出。从您已有的代码来看,gpperfmon_install 似乎是一个交互式应用程序,这是另一个问题。有时您可以使用此处文档,但在其他情况下您需要使用 expect

su gpadmin -c 'gpperfmon_install --enable --password password --port 5432 << EOF
y
gpstar
y
EOF'

You need to use the -c option to su in order for it to run a command and then exit. With the options you have given to su, it will run gpadmin's default shell, which will block until the shell has exited. Judging by the code you already have, it appears that gpperfmon_install is an interactive application, which is another issue. Sometimes you can use a here document, but in other cases you will need to use expect.

su gpadmin -c 'gpperfmon_install --enable --password password --port 5432 << EOF
y
gpstar
y
EOF'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文