MacOSX how to autorun a script after su - root?

微笑、不失礼 提交于 2019-12-12 05:17:33

问题


Lets assume i am normal user, the i will switch to root:

user ~ $ su - root
Password: 
root ~ #

So once i logged in as root, i want to run following command automatically:

source .bash_profile

How can i have that above command run automatically please?


回答1:


According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

In your case, you don't need to source .bash_profile like this.

You just need to put source .bash_profile in your root's .bashrc file

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile
fi

Read me for better understanding of .bash_profile and .bashrc

Update

Example:

[root@sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root@sgeorge-ld ~]# exit
logout
[sgeorge@sgeorge-ld ~]$ su - root
Password: 
Testing .bashrc for a stack query
[root@sgeorge-ld ~]# 



回答2:


First of all, when you switch to root user, you will be still your regular user's home directory. Which .bash_profile you want to execute? /Users/myuser/.bash_profile or root's /var/root/.bash_profile?

Regardless of what you would like to execute, you can edit /var/root/.bashrc (if you don't have it, create one) and add your command there.



来源:https://stackoverflow.com/questions/14680333/macosx-how-to-autorun-a-script-after-su-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!