I need to run a script every time I open WSL Ubuntu 18.04 on Windows 10 - sh

Every time I open WSL Ubuntu 18.04 on Windows 10 I want to run these settings automatically.
alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'
export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds
export DISPLAY=localhost:0.0
I tried making .sh script with the following content in /etc/init.d/ but it didn't work.
#!/bin/bash
alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'
export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds
export DISPLAY=localhost:0.0

To run these commands every time you open WSL, you will want to append the commands to .bashrc.
In bash, run
echo "alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'" >> ~/.bashrc
echo "export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds" >> ~/.bashrc
echo "export DISPLAY=localhost:0.0" >> ~/.bashrc

To create an environment variable which will be visible for all users on Ubuntu you can create a sh file in /etc/profile.d folder.
In example :
sudo vi /etc/profile.d/my_vars.sh && sudo chmod o+r /etc/profile.d/my_vars.sh
then include there your variables. For example:
export ORACLE_HOME="/opt/oracle/instantclient_11_2"
terminate and start wsl again. Variables should be accessible for all users.

Related

Permanently set PGDATA environment variable Mac

To start my Postgres server I need to run these commands:
pg_ctl -D /usr/local/var/postgres start
export PGDATA='/usr/local/var/postgres'
Isn't there a way to define the PGDATA permanently and from there on only use commands like pg_ctl start to operate in the shell? Thanks.
You're half way there. The only thing you might want to do is to get PGDATA whenever you open the terminal by adding the export line to your shell config file. On my macOS machine i added the following line:
export PGDATA='/Users/john/.postgres' to ~/.bash_profile file. Don't forget to source it if you want to see the effect immediately by executing . ~/.bash_profile command. That's it from now on you can start your posters server by simply typing pg_ctl start. Hope it was worth the wait for the answer. :)
export will set the context for your current session. So we need to hack our terminal so that we will export this PGDATA env variable to every session.
If you are using zsh, you can simply execute the below command.
echo "export PGDATA='/usr/local/var/postgres'" >> ~/.zshrc
In case you are not using zsh replace ~/.zshrc in the above command with ~/.bashrc or ~/.profile
then, do source ~/.zshrc to see the immediate effect. You can instantly check using echo $PGDATA command.
Whenever you open a new terminal, your environment variable will be available by default.

How can the terminal in Jupyter automatically run bash instead of sh

I love the terminal feature and works very well for our use case where I would like students to do some work directly from a terminal so they experience that environment. The shell that launches automatically is sh and does not pick up all of my bash defaults. I can type "bash" and everything works perfectly. How can I make "bash" the default?
Jupyter uses the environment variable $SHELL to decide which shell to launch. If you are running jupyter using init then this will be set to dash on Ubuntu systems. My solution is to export SHELL=/bin/bash in the script that launches jupyter.
I have tried the ultimate way of switching system-wide SHELL environment variable by adding the following line to the file /etc/environment:
SHELL=/bin/bash
This works on Ubuntu environment. Every now and then, the SHELL variable always points to /bin/bash instead of /bin/sh in Terminal after a reboot.
Also, setting up CRON job to launch jupyter notebook at system startup triggered the same issue on jupyter notebook's Terminal.
It turns out that I need to include variable setting and sourcing statements for Bash init file like ~/.bashrc in CRON job statement as follows via the command $ crontab -e :
#reboot source /home/USERNAME/.bashrc && \
export SHELL=/bin/bash && \
/SOMEWHERE/jupyter notebook --port=8888
In this way, I can log in the Ubuntu server via a remote web browser (http://server-ip-address:8888/) with opening jupyter notebook's Terminal default to Bash as same as local environment.
You can add this to your jupyter_notebook_config.py
c.NotebookApp.terminado_settings = {'shell_command': ['/bin/bash']}
With Jupyter running on Ubuntu 15.10, the Jupyter shell will default into /bin/sh which is a symlink to /bin/dash.
rm /bin/sh
ln -s /bin/bash /bin/sh
That fix got Jupyter terminal booting into bash for me.

Automation of Cygwin configuration with PowerShell

I have installed Cygwin using PowerShell scripting.
I am doing the following step manually:
Running a new cygwin bash shell (after the edit of cygwin.bat) and enter:
mount --change-cygdrive-prefix /
chmod +r /etc/passwd /etc/group
chmod 755 /var
Start Cygwin bash shell and run ssh-host-config. Answer yes to all the key generation questions.
Is it possible to automate these things in PowerShell scripts, like installing Cygwin, then doing steps 1 and 2 in a single shot?
Use this command:
bash.exe ssh-host-config --yes -u "Cygwinuser" -c "binmode ntsec tty" -w "pwd#123"
cygrunsrv -S sshd
Later go to services.msc to check if the service is running or not

PostgreSQL command psql not found, trouble adding to $PATH

I am trying to add PostgreSQL to my $PATH variable. I tried this to see where psql is
whereis psql
To which I got a null response. I do have the PostgreSQL.app installed so I clicked on "Open psql" from the GUI and it prompted the Terminal to open and display:
/Applications/Postgres.app/Contents/MacOS/bin/psql; exit;
So I tried to add the above to the $PATH variable in my ~/.bash_profile (which may have its own problems since I don't know if you can add paths with .app extensions to the $PATH) but when I restart my Terminal and do echo $PATH | grep Postgres.app I get nothin'.
Here's an approach to take help isolate problems you may have.
Step 1: See if you can add PostgreSQL to your PATH without using Bash dot files.
$ export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH;
$ which psql
If this works...
Step 2: Verify that ~\.bash_profile is being sourced when a new user session is started.
Add the following to the end of your ~/.bash_profile:
echo "From bash_profile.";
Now restart Terminal.app or iTerm and see if that message appears about your prompt.
If this works...
Step 3: Add PATH to ~/.bash_profile.
In ~/.bash_profile:
export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH;
Restart your terminal and run:
$ which psql
If you're not seeing:
/Applications/Postgres.app/Contents/MacOS/bin/psql
Then it might be time to scrap trying to install PostgreSQL as a Mac package and use Homebrew.
NOTE: It's psql and NOT pgsql.
From the Postgres documentation page:
sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
restart your terminal and you will have it in your path.

java_home environment variable in linux not found

I'm trying to add java_home in linux machine (centos 5.8)
I'm adding this part to setting JAVA_HOME and PATH for all users in my machine
vi /etc/profile
export JAVA_HOME=/opt/jdkx.x.x_xx
export PATH=$PATH:$JAVA_HOME/bin
after seting it up, i try to verify it by using echo command
echo $JAVA_HOME
but it does not give me any path.Is there something wrong with my configuration?
This method will persist OS updates
echo "export JAVA_HOME=/usr/java/default/" > /etc/profile.d/java_home.sh
If you have more then on version of java install there may be trouble.
The JAVA_HOME path is different because we often install different version JDK and maybe different places. Once a I try to find the general way. There is the result.
Firstly, to query the installed jdk package name: rpm -qa|grep java
my result is:java-1.6.0-openjdk
Secondly, to query the installed place of this package
rpm -ql java-1.6.0-openjdk
Most files is under: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/
go there to confirm it is a real JDK directory
Thirdly, execute export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/ in terminal. Or add it /etc/bashrc for all user.
Question has been edited, answer doesn't make sense anymore.
Left as a placeholder for comments.
echo $SHELL will tell you which shell you are using. Most likely /bin/bash.
Assuming bash, /etc/profile is only read for a login shell (bash --login), not just a new interactive shell.
i.e. if you
sh1% vi /etc/profile
sh1% bash # /etc/profile not read
sh2% echo $JAVA_HOME
sh2%
sh1% vi /etc/profile
sh2% bash --login # /etc/profile should be read.
sh2% echo $JAVA_HOME
/opt/blah/blah/blah
sh2%