How to auto run commands when log on to Windows Subsystem for Linux - windows

I have a Ubuntu 20.04 running within WSL 2 on a Windows 10 computer.
Every time I login to Ubuntu, I had to manually execute these four line by pasting it one by one in the Windows 10 Terminal.
sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
exec sudo nsenter -t $(pidof systemd) -a su - $LOGNAME
sudo /etc/init.d/xrdp start
May I know if there is a way to skip this manual process?

You can use .bashrc file to execute commands whenever you open the terminal. It should be located at $HOME directory.
cd $HOME
nano .bashrc
place your commands at the end of the file, press ctl+x then y to save.

Related

Running powershell script inside a bash script

In Powershell-
How do I run a powershell script(.ps1) inside a bash script(.sh)
I can run just the powershell script-
& .\scriptfile.ps1
and just the bash script.
But when I try to run the powershell inside the bash script, I get
file.ps1 : command not found
Both scripts are in the same path.
Are you trying
.\scriptfile.ps1
?
That should be
./scriptfile.ps1
But also, when invoking powershell from a bash script, you'll need to either run the pwsh command like
pwsh ./scriptfile.ps1
or the first line of your Powershell script file should be a shebang (interpreter directive) like:
#!/usr/bin/env pwsh
See How can I use a shebang in a PowerShell script?
I'm using git bash on Windows 10 Pro and there it's powershell.
The executable is in /c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe and it's contained in my $PATH.
So I can say
powershell ./scriptfile.ps1
# or inline:
powershell 'my script'
In the Ubuntu-on-Windows bash I have to say
powershell.exe ./scriptfile.ps1
try to change permission and make it excutable with chmod +x file.ps1
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

Run Terminal at startup and execute command as sudo

I'm trying to run an executable file as sudo using terminal at startup but I'm having some issues. I couldn't find the solution in other answers, so I opened up this one. I'm using a Raspberry Pi 3 B+ with the Raspbian Stretch with desktop and recommended software downloaded from the official Raspberry Pi website.
I have an executable that needs to be run with sudo (I use the pigpio library to communicate with another hardware through SPI and if I don't run the executable with sudo, the pigpio doesn't work). What I'm trying to achieve is that when the Pi finishes to startup the graphical interface, it would run the LXTerminal and execute "sudo home/pi/myfolder/myprogram".
I've tried:
sudo nano ~/.config/autostart/myprogram.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=myprogram
Exec=lxterminal -e "sudo /home/pi/myfolder/myprogram"
Terminal=true
sudo chmod a+r ~/.config/autostart/myprogram.desktop
Also tried:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
lxterminal --command="sudo /home/pi/myfolder/myprogram"
Both methods did open the lxterminal at startup, but didn't executed my program.
Can anyone help me out?
I found the solution, so I'm gonna post it here in case someone else needs it. If it already exists in somewhere else around here, feel free to tag it as duplicated.
The solution that worked for me was this:
sudo nano ~/.config/autostart/myprogram.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=myprogram
Exec=lxterminal -e bash -c 'sudo /home/pi/myfolder/myprogram;$SHELL'
Terminal=true
sudo chmod a+r ~/.config/autostart/myprogram.desktop
The $SHELL makes the terminal stay open after myprogram ends its execution. If you don't need this feature, just exclude the ;$SHELL part of the code above.

kubectl bash completion doesn't work in ubuntu docker container

I'm using kubectl from within a docker container running on a Mac. I've already successfully configured the bash completion for kubectl to work on the Mac, however, it doesn't work within the docker container. I always get bash: _get_comp_words_by_ref: command not found.
The docker image is based on ubuntu:16.04 and kubectl is installed via the line (snippet from the dockerfile)
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && \
mv kubectl /usr/local/bin
echo $BASH_VERSION gives me 4.3.48(1)-release, and according to apt, the bash-completionpackage is installed.
I'm using iTerm2 as terminal.
Any idea why it doesn't work or how to get it to work?
Ok, I found it - I simply needed to do a source /etc/bash_completion before or after the source <(kubectl completion bash).
check .bashrc
enable programmable completion features (you don't need to enable
this, if it's already enabled in /etc/bash.bashrc and /etc/profile
sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
A Linux container executed on macOS creates a separate environment and
yes, it looks like a thread from macOS shell, but it is not. Shell history,
properties, functions are a different story.
Moreover, if the container has no persistent volume mounted all of those parameters will be transisten and won’t survive container’s restart.
The approach to have bash completion of both of them - macOS and Ubuntu
Linux are similar, but require different steps to take:
macOS side - permanent support for kubectl bash completion:
use homebrew to install support:
brew install bash-completion
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
Ubuntu container’s approach to have kubectl and bash completion support build in:
You can adapt this set of commands and use it in Dockerfile during the image preparation:
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubectl
echo 'source <(kubectl completion bash)' >> ~/.bashrc
If afterwards you or user executes /bin/bash in running container then you should get completion working.
docker exec -it docker_image_id /bin/bash
this will start bash shell with the bash completion.
I united two top comments for Ubuntu 22.04
edit ~/.bashrc and add
source /etc/bash_completion
before
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

Perlbrew installation through vagrant provision.sh

I want to automate the installation of perlbrew into the vagrant box. I use .sh file to accomplish this.
provision.sh
apt-get update
sudo -H -u vagrant bash -c " \curl -kL https://install.perlbrew.pl | bash"
sudo -u vagrant bash -c "source ~/perl5/perlbrew/etc/bashrc"
After ssh into the vagrant i expect that
$ which perlbrew
will return
/home/vagrant/perl5/perlbrew/bin/perlbrew
but unfortunately it returns nothing.
There is no way the settings applied by your source ~/perl5/perlbrew/etc/bashrc command would be visible in another bash session (and a SSH session executes a new bash process).
You need to add the command source ~/perl5/perlbrew/etc/bashrc to one of the bash "rc" files.
For a single user with the following command:
echo "source ~/perl5/perlbrew/etc/bashrc" >> ~/.bashrc
For all users with the following command:
echo "source ~/perl5/perlbrew/etc/bashrc" >> /etc/bash.bashrc
This way every time a new bash session is started, it will run source ~/perl5/perlbrew/etc/bashrc and apply the settings.

Bash Scripting; giving commands to programs stdin

I am very new to bash scripting. I have the following script:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot/
apt-get update
apt-get --simulate install $a > output
I actually want the last 2 comands to be run in chroot environment but I do not know how to give it to it, I searched but I could not find. I also want chroot to exit after execution of the commands, but it currently hangs. What can I do to prevent this?
EDIT: For future visitors:
cp /etc/apt/sources.list /var/chroot/etc/apt/sources.list
chroot /var/chroot apt-get update > /dev/null
chroot /var/chroot apt-get --simulate install nodejs
The command you want to run in the chroot environment must be given to chroot as an argument. See the manual page.

Resources