2016-12-01 85 views
0

我有一个脚本,我手动运行(比方说)mylogin。但有一行需要以用户postgres运行。如何作为特定用户运行一行bash shell脚本

什么是一个很好的方法来做到这一点?

没关系,如果我得到密码提示。我只是需要它以某种方式工作。

这里是我迄今为止...

~/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。

回答

2

其中一个论据是须藤命令,所以你可以这样做:

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

其中-u <user>改变你的目标用户

+0

优秀。谢谢。 – Ethan