2016-12-01 84 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