How can I execute commands as another user with their .bash_profile instead of the current user's?

StackOverflow https://stackoverflow.com/questions/9394175

문제

I want to run a command as a not-root user in a startup script (rc.local). The command I want to execute is actually an alias in the non-root user's .bash_profile. I have tried to use

su - myuser -c aliased_cmd

but this doesn't work. If I simply type

su - myuser
# wait for login...
aliased_cmd

it works fine, but obviously this isn't appropriate in the actual script. Of course I could copy the aliases and functions I'd like to use into the actual rc.local file, but this seems a bit silly... perhaps there is an option I don't know about or a way to use sudo for this?

도움이 되었습니까?

해결책

after sourcing the profile, include the following

shopt -s expand_aliases

다른 팁

Quoting man su:

When - is used, it must be specified as the last su option.

It doesn't specify what happens if it's not the last option. Also, in the synopsis the username is after all the options.

Have you tried su -c aliased_cmd - myuser or su -l -c aliased_cmd myuser?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top