简体   繁体   English

如何在bash脚本中使用两个不同的用户

[英]How to Use two different users in bash scripting

Hi everyone, I am writing a bash script on RedHat V5.1. 大家好,我正在RedHat V5.1上编写bash脚本。 In this script i have to run one command using dbAdmin and right after that i have to switch again to root user to run the other commands. 在此脚本中,我必须使用dbAdmin运行一个命令,此后,我必须再次切换到root用户来运行其他命令。 Can anybody tell me how i can do this in bash scripting. 谁能告诉我如何在bash脚本中做到这一点。

Thanks in advance.... 提前致谢....

You need to run your script as root.Then in any part of code just do this: 您需要以root用户身份运行脚本,然后在代码的任何部分中只需执行以下操作:

su - dbadmin -c "command"

Replace command with whatever you want to run under dbadmin user. 将命令替换为要在dbadmin用户下运行的命令。

This is exact way how redhat init scripts run services under specific users, eg. 这是Redhat初始化脚本在特定用户(例如)下运行服务的确切方式。 oracle DB 甲骨文数据库

Try using the SUDO command to switch between root and your dbAdmin account. 尝试使用SUDO命令在root用户和dbAdmin帐户之间切换。

http://ubuntuforums.org/showthread.php?t=826935 http://ubuntuforums.org/showthread.php?t=826935

http://linux.die.net/man/8/sudo http://linux.die.net/man/8/sudo

# command1 ; command2 ; sudo -u dbAdmin command3 ; command4 ; command5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM