linux – 以不同的用户身份运行shell脚本
发布时间:2020-12-13 18:44:24 所属栏目:Linux 来源:网络整理
导读:将 shell脚本作为不同用户运行的好方法是什么.我正在使用Debian etch,我知道我想要冒充哪个用户. 如果我是手动操作,我会这样做: su postgres./backup_db.sh /tmp/testexit 由于我想自动化该过程,我需要一种方法来运行backup_db.sh作为postgres(继承环境等)
将
shell脚本作为不同用户运行的好方法是什么.我正在使用Debian etch,我知道我想要冒充哪个用户.
如果我是手动操作,我会这样做: su postgres ./backup_db.sh /tmp/test exit 由于我想自动化该过程,我需要一种方法来运行backup_db.sh作为postgres(继承环境等) 谢谢! 解决方法
要将您的脚本作为另一个用户作为一个命令运行,请运行:
/bin/su -c "/path/to/backup_db.sh /tmp/test" - postgres Breaking it down: /bin/su : switch user -c "/path/to..." : command to run - : option to su,make it a login session (source profile for the user) postgres : user to become 我建议总是在这样的脚本中使用完整路径 – 你不能总是保证在你su时你会在正确的目录中(也许有人改变你的homedir,谁知道).我也总是使用su(/ bin / su)的完整路径,因为我是偏执狂.有可能有人可以编辑您的路径并导致您使用受损版本的su. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |