如何作为特定用户运行一行Bash Shell脚本



我有一个我手动运行的脚本(假设)mylogin。但是有一行需要以用户postgres

运行

做什么的好方法?

如果我收到密码提示,则可以。我只需要以某种方式工作。

这是我到目前为止所拥有的...

~/reload_test_data.sh

#!/bin/bash
# Here's the part that needs to run as user `postgres`...
sudo su postgres
export PGDATA=/Library/PostgreSQL/9.4/data && pg_ctl -m fast restart
# And here we should go back to `mylogin`...
cd ~/projects/my_project
echo 'Dropping database'
bundle exec rake db:drop
# More stuff etc...

我正在使用Mac OS 10.12.1。

sudo的参数之一是命令,因此您可以执行以下操作:

sudo -u <user> bash -c "command_1; command_2; etc"

-u <user>更改为目标用户

最新更新