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