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

I want to run the command as a non-root user when running script (rc.local). The command I want to execute is actually an alias for the root user -bb_profile. I tried to use

su - myuser -c aliased_cmd 

but it does not work. If I just type

 su - myuser # wait for login... aliased_cmd 

it works fine, but obviously this does not fit in the actual script. Of course, I could copy the aliases and functions that I would like to use in the actual rc.local file, but that seems a little silly ... maybe there is an option that I don't know about, or a way to use sudo for this?

+4
source share
2 answers

after searching for a profile, include the following

 shopt -s expand_aliases 
+2
source

Quote from man su :

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

It does not indicate what will happen if this is not the last parameter. In addition, in the short description, the username is after all the parameters.

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

0
source

Source: https://habr.com/ru/post/1397788/


All Articles