running shell script starts new shell instead of running the shell script - bash

I don't know what to title this. Title suggestions welcome.
I'm using zsh as my default shell for the user I'm using. Whenever I try and run a script that uses bash it starts a new login shell of zsh, rather than running the script. If I change the #!/bin/bash line to #!/usr/bin/zsh it just works. Here's the kicker. Once that new shell is started the script starts working fine from that point on.
I'm using mate-terminal with vcxsrv as the X server. I initially start up a plain ubuntu bash terminal, which I then use to spawn mate-terminal, as I need the DISPLAY var setup and what not before starting mate-terminal. Anyhow, here's the script I'm attempting to start.
It's also important to note that if I reverse the shells by making bash default, and using zsh inside the script, the same thing does not happen.
How do I make this just run the script?
$ cat tmp.sh
#!/bin/bash -x
echo hello
$ ./tmp.sh
+ source /usr/sbin/start-systemd-namespace
++ SYSTEMD_EXE='/lib/systemd/systemd --system-unit=basic.target'
+++ ps -eo pid=,args=
+++ awk '$2" "$3=="/lib/systemd/systemd --system-unit=basic.target" {print $1}'
++ SYSTEMD_PID=562
++ '[' myuser '!=' root ']'
++ '[' -z 562 ']'
++ '[' 562 '!=' 1 ']'
++ export
++ sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}'
++ grep -E -v '^(BASH|BASH_ENV|DIRSTACK|EUID|GROUPS|HOME|HOSTNAME|IFS|LANG|LOGNAME|MACHTYPE|MAIL|NAME|OLDPWD|OPTERR|OSTYPE|PATH|PIPESTATUS|POSIXLY_CORRECT|PPID|PS1|PS4|SHELL|SHELLOPTS|SHLVL|SYSTEMD_PID|UID|USER|_)(=|$)'
++ export 'PRE_NAMESPACE_PATH=/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.rbenv/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.nvm/versions/node/v14.12.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2020.812.0_x64__79rhkp1fndgsc:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Android:/mnt/c/Windows/System32:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/trent/AppData/Local/Microsoft/WindowsApps:/home/myuser/go/bin:/home/myuser/.local/bin:/home/myuser/go/bin:/home/myuser/.local/bin'
++ PRE_NAMESPACE_PATH='/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.rbenv/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.nvm/versions/node/v14.12.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2020.812.0_x64__79rhkp1fndgsc:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Android:/mnt/c/Windows/System32:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/trent/AppData/Local/Microsoft/WindowsApps:/home/myuser/go/bin:/home/myuser/.local/bin:/home/myuser/go/bin:/home/myuser/.local/bin'
+++ pwd
++ export PRE_NAMESPACE_PWD=/home/myuser
++ PRE_NAMESPACE_PWD=/home/myuser
++ exec sudo /usr/sbin/enter-systemd-namespace ''
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon Oct 12 21:48:02 MDT 2020
System load: 0.0 Processes: 66
Usage of /: 7.2% of 250.98GB Users logged in: 0
Memory usage: 7% IPv4 address for eth0: 172.26.153.138
Swap usage: 0%
* Kubernetes 1.19 is out! Get it in one command with:
sudo snap install microk8s --channel=1.19 --classic
https://microk8s.io/ has docs and details.
21 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Mon Oct 12 21:42:37 MDT 2020 on pts/3
* keychain 2.8.5 ~ http://www.funtoo.org
* Found existing ssh-agent: 108201
* Known ssh key: /home/myuser/.ssh/id_rsa

The key was this line in /etc/bash.bashrc
source /usr/sbin/start-systemd-namespace
Awhile back I got systemd working from some instructions elsewhere on the net. It must have added this line. It causes an entirely new shell to start when you're using another shell like zsh. After commenting it out, voila, no more problem.

Related

Ubuntu 19.10 .. can't change shell to zsh

I'm trying various things but I can't change shell to zsh in Ubuntu 19.10 x64 on my MacBook Pro 2012 with 16 GB RAM.
$ which zsh # Output: /usr/bin/zsh
$ zsh --version # Output: zsh 5.7.1 (x86_64-ubuntu-linux-gnu)
$ chsh -s $(which zsh)
It makes changes to /etc/passwd file but when I run
$ echo $SHELL
It still gives me /bin/bash.
What am I doing wrong?

Start nginx from shell script on crontab

I want to check NGINX is running or not every 1 minute.
My shell script is:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
/etc/init.d/nginx start
else
echo "NGINX is running"
fi
Script run with sh launch.sh correctly (If NGINX is not running, run NGINX).
The problem is when I want to run the script every 1 minute by crontab, nothing happens. Crontab list is here:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * ~/sh launch.sh
I test * * * * * sh launch.sh, * * * * * launch.sh and * * * * * ./launch.sh but none of them work correctly.
My OS is UBUNTU 18.04.
This is log:
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3239]: (root) CMD (~/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3240]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3238]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3237]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3374]: (root) CMD (~/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3373]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3376]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3372]: (CRON) info (No MTA installed, discarding output)
I think the command fired but nothing happend.
NGINX needs sudo privilege.
If you have sudo privileges you can modify /etc/sudoers.d/username file and execute sudo commands without password.
The file usually contains a user and a list of commands that the user can run without having to specify a password. In your case, you can run:
sudo /etc/init.d/nginx start
Add or modify your sudoers file. (replace username with your username.)
$ EDITOR=nano sudo visudo -f /etc/sudoers.d/username # EDITOR=nano sets my editor (because I am more comfortable with nano)
Copy and paste following.
You can add more sudo commands separating by comma.
username ALL=(ALL) NOPASSWD: /etc/init.d/nginx start,/etc/init.d/nginx start
Note: Commands will only execute called with sudo.
Prepend sudo in your launch.sh:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
sudo /etc/init.d/nginx start
else
echo "NGINX is running"
fi
Make file executable.
$ chmod +x launch.sh
~ won't be expanded the way it is in an interactive shell when in a crontab. Use /home/username instead.

Unable to modify /etc/shells on macOS to include brew installed version of bash

I am trying to update my /etc/shells file to include the path to a homebrew installed version of bash which resides at /usr/local/bin/bash
$ sudo echo /usr/local/bin/bash >> /etc/shells returns Permission denied and attempting to manually update is not allowed as it appears to be read-only.
Upon inspecting the file, the permissions are set as follows:
-rw-r--r-- 1 root wheel 179 Feb 21 2017 /etc/shells
So, with this in mind, and after looking at this article about Updating you shell with Homebrew I tried to initiate a shell as the root user and then try command above, i.e:
$ sudo -s
$ echo /usr/local/bin/bash >> /etc/shells
$ chsh -s /usr/local/bin/bash
However, this seems to hang on the first command ($ sudo -s). This spawns a bash process that eats up ~ 70% CPU and nothing happens.
Is there an alternative way one can update the /etc/shells/ file?
An approach to adding to a root-only file is echo /usr/local/bin/bash | sudo tee -a /etc/shells.
– Petesh
Would you be able to explain why that works and the sudo echo /usr/local/bin/bash >> /etc/shells does not though.
The latter doesn't work because the output redirection >> is (tried to be) applied by the shell before the sudo … is executed, and of course the user shell has no permission to do that.
Or you can just use this (I had to do this on macOS Mojave):
sudo sh -c "echo $(which zsh) >> /etc/shells"
chsh -s $(which zsh)

Running script with ssh

I work with machine A and I want to run a script that exists in machine B.
I did the ordinary command:
ssh user#machine_B_adress '. script.sh'
the problem is that I used in the script some commands that cannot be interpret with machine A. So I get command not found: (for example)
ksh: sqlplus: not found
I tried to open a shh by:
ssh user#machine_B_adress
and then run the script, it works!!!
Assuming that the shell for user#machine_B is bash, the first example 'ssh user#machine_B_adress '. script.sh', bash sets up the shell env differently for interactive/non-interactive sessions.
See man bash about interactive shells
Looks like you can emulate the interactive environment by adding a bash -l -c
$ ssh user#machine_B_address "bash -l -c '. script.sh'
My quick test, I added debug echo to .bash_profile of the remote user
$ ssh foouser#jdsrpi1 "bash --login -c '. foo.sh'"
This is file .bash_profile
foouser
SHELL = /bin/bash
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
Similar scenario for ksh
$ ssh kshuser#jdsdrop1.jimsander.io "date"
Tue Apr 18 11:52:43 EDT 2017
$ ssh kshuser#jdsdrop1 "ksh -l -c date"
This is SHELL(/usr/bin/ksh) file .profile
Tue Apr 18 11:53:24 EDT 2017

Need to run chromium as normal user from root script

I have a kiosk that shuts down every day using rtcwake, and this uses root user. I've used && to execute the boot script after rtcwake completes, however it then starts the browser as root causing problems.
This is the command I use:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && sudo -u debian -i bash $HOME/kiosk/bin/startup.sh &.
The sudo command does work to some extent. It calls the debian user, and executes the correct script, however, it still screws up my chromium preferences.
Here is the startup script:
echo debian | sudo -S hwclock -w
export HOME=/home/debian
#log boot time
echo "Booting at" $(date) >> $HOME/kiosk/bin/logs/boot.log
#echo debian | sudo -S service connman restart
echo debian | sudo -S at 15:30 -f $HOME/kiosk/bin/shutdown.sh
crontab -u debian crontab.txt
bash $HOME/git.sh
#sudo -i -u debian
#start kiosk
export DISPLAY=:0
chromium-browser --kiosk --disable-gpu
http://localhost/kiosk/Client/main.html &
#update ip
bash /home/debian/git.sh &
I'm wondering what could be causing chrome to be executed as root. I have no idea what is going wrong.
If you execute a command with sudo it will not change environment variables like $HOME. Since per user settings are stored in $HOME, this affects the executed program if it needs such configuration files. Check this for example:
sudo -u debian bash -c 'echo $HOME'
It will print the home folder of the calling user, not the home folder of the user specified trough -u. The sudo command supports the -H command line option to handle this, however if it works depends on the security police in use.
As a solution you can use the su command instead of sudo in this case:
... && su debian -c chromium
Since su itself is executed by root you won't be asked for the password.
You must enter a password to log into a new user shell.
The command needs to be modified as follows:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && echo debian | sudo -S -u debian -i bash $HOME/kiosk/bin/startup.sh &
This avoids needing a password to log in as normal Debian user, and executes the script.

Resources