1

I use a shell script to provision my server. After I modify the .bashrc file, i need to exit then log back in to restart the shell.

su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "alias mkvirtualenv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

source ~/.bashrc

// this is where I need help, i need to exit the shell and relogin. then run mkvirutalenv command.
mkvirtualenv test1

EOF

Update:

this is the shell script file vagrant will run.

#!/usr/bin/env bash

if [ -f "/var/vagrant_provision" ]; then
    exit 0
fi

echo "Installing Flask environment and setting it up.."
echo "------------------------------------------------"
apt-get update >/dev/null 2>&1
echo "1. update is done"

#apt-get upgrade -y >/dev/null 2>&1
echo "2. upgrade is done -- skipped for dev"

rm -rf /var/www
ln -fs /vagrant /var/www
echo "3. Symbolic link is created"

apt-get install -y build-essential python-dev >/dev/null 2>&1
apt-get install -y curl >/dev/null 2>&1
echo "4. curl is installed"

apt-get install -y python-pip >/dev/null 2>&1
echo "5. pip is installed"

pip install virtualenv virtualenvwrapper >/dev/null 2>&1
echo "6. virtualenv and virtualenvwrapper are installed"

su vagrant <<'EOF'
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "export PROJECT_HOME=/var/www" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
echo "alias mkv='mkvirtualenv --no-site-packages'" >> ~/.bashrc
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc

mycmd
mkv test1
EOF

echo "7. add environment variables to .bashrc"
echo "8. source .bashrc"
echo "9. test1 environment is created"

touch /var/vagrant_provision
echo "------------------------------------------------"
echo "Installation is done"

this is the output I got. still getting command not found.

Installing Flask environment and setting it up..
------------------------------------------------
1. update is done
2. upgrade is done -- skipped for dev
3. Symbolic link is created
4. curl is installed
5. pip is installed
6. virtualenv and virtualenvwrapper are installed
bash: line 8: mycmd: command not found
bash: line 9: mkv: command not found
7. add environment variables to .bashrc
8. source .bashrc
9. test1 environment is created
------------------------------------------------
Installation is done
4
  • why you want to exit then log back in to restart the shell? You are using source command. So all the changes will get reflected.also if you really want to use new shell the you can use bash mkvirtualenv test1. Commented Oct 27, 2013 at 14:58
  • @virus I have to exit and log back in. otherwise, I will get this error message "mkvirtualenv: command not found", and "bash mkvirtualenv test1" didnt work. Commented Oct 27, 2013 at 15:35
  • #!/usr/bin/sh echo "alias mycmd='ls'" >> ~/.bashrc source ~/.bashrc mycmd I am running this which works fine. Why not in ur case? Commented Oct 27, 2013 at 15:47
  • I am sure that there is some problem with setting path. do one thing to debug it.change "alias mkvirtualenv" to "alias myvirtual". then chack what happens? Commented Oct 27, 2013 at 15:57

1 Answer 1

3

==> After I modify the .bashrc file, i need to exit then log back in to restart the shell.

No need to restart the shell. If you want changes to get reflected in current session immediately then you can do this by using below commands.

source ~/.bashrc

or

. ~/.bashrc

By doing this you will load current new settings into your session. So you need not to re-login.

Please find one sample code which work properly.

#!/usr/bin/sh
echo "alias mycmd='ls'" >> ~/.bashrc
source ~/.bashrc
mycmd

To fix your problem -->

Please create passwordless ssh for user 'vagrant'. Please check the documantation to create passwordless ssh here.

Then put your run document commands like below.

ssh vagrant@localhost "alias mycmd='echo $HOME';/mycmd"

here using '/' before mycmd is mandatory otherwise 'mycmd' will be executed by current shell only and you will get command not found error.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your help. I just edit my post and post the whole shell script file there. still getting command not found error. the only thing I can think of is I wrap those commands in "su vagrant <<'EOF'".
@feelexit-- yes..here only is the issue. please use runuser.cyberciti.biz/open-source/command-line-hacks/…
@feelexit-- you can not use 'runuser' command. Please check the latest answer. It will definitely fix your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.