【问题标题】:How can I run one line of bash shell script as a specific user如何以特定用户身份运行一行 bash shell 脚本
【发布时间】:2016-12-01 18:31:54
【问题描述】:

我有一个手动运行的脚本(比如说)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。

【问题讨论】:

    标签: bash shell sudo


    【解决方案1】:

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

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

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

    【讨论】:

    • 优秀。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2018-05-11
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多