Different behaviour with sudo and without - bash

I have simple file opening spotify.
#!/bin/sh
spotify 1>/dev/null 2>&2 &
OR (the same result)
#!/bin/sh
spotify &
When I run command sudo sh test.sh message in console:
mkdir: cannot create directory '/run/user/0': Permission denied
No protocol specified
(spotify:23946): Gtk-WARNING **: 13:14:26.995: cannot open display: :0
And when I run sh test.sh spotify opens normally without any error.
Can anyone help me understand why it behaves like this? What's the reason and how I can execute command differently with sudo?

Related

Running into error with BASH script with permission denied, but when running the command directly in bash shell its getting executed

When running below command directly in bash shell, I am able to get the output. But when I am passing it via BASH script getting access denied. Any help would be appreciated
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Error:
$ sh test.sh
test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
I think the reason is that when executing the script with sh test.sh we're asking the POSIX interpreter (shell) to execute the content on the script, while when executing it with ./test.sh we're asking the script to "execute itself".
For the latter, the file needs to have execution permissions, which you can add with
chmod +x test.sh
Issue was with the naming convention of JQ inside BASH folder path, because of which the script was unable to pick the command. Renaming the JQ within BASH folder resolved this

Exporting ROS Master URI from shell script

I am trying to export ROS_MASTER_URI from a shell script and then launch roscore. In my .sh file I have:
roxterm --tab -e $SHELL -c "cd $CATKIN_WS; $srcdevel; export ROS_MASTER_URI='http://locahost:1234'; roscore -p 1234"
When I do this, however, I get the following error in the roscore tab:
WARNING: ROS_MASTER_URI [http://locahost:1234] host is not set to this machine.
When I echo the ROS_MASTER_URI in this tab, it says that it is localhost:1234, which is correct. When I manually execute these commands, it works correctly and roscore launches without any issues. I am not sure why it does not work when launched from a bash file.
It was just a typo- missed the l in localhost. All working now.

How to open a new terminal and run two linux commands?

I have create a .sh script, with option to open terminal and that same terminal run 2 commands.
I know to open a new window or tab and run a command but when I put && I receive error like this by using this command:
Commands:
gnome-terminal --tab -- "ls && clear"
Error:
There was an error creating the child process for this terminal
Failed to execute child process “ls && clear” (No such file or directory)
My OS is Linux(Ubuntu)
gnome-terminal is expecting a command name. It looks for a command literally called ls && clear but can't find it. You need to run it in a shell:
gnome-terminal --tab -- bash -c "ls && clear"

Call remote sh script from local sh file

I have a problem with running sh scripts on CentOS which calls remote sh file. On user#host1 I have start.sh file with next command inside
NODE1_SSH_PATH=user#host2
PROGRAM_HOME=/home/user/app
ssh $NODE1_SSH_PATH $PROGRAM_HOME/bin/run.sh > start.log
Result of this script is next:
bash: /home/user/app/bin/run.sh: Permission denied
I tried run this script with chmod like this:
ssh $NODE1_SSH_PATH chmod u+x $PROGRAM_HOME/bin/run.sh > start.log
But in this case I have't got any result, log file is empty to. Could someone help me to slow this I hope simple task?
I believe /home/user/app/bin/run.sh is not executable.
try this
ssh $NODE1_SSH_PATH /bin/bash $PROGRAM_HOME/bin/run.sh > start.log

Running individual commands works but not when combines in a shell script.. Why?

I need to run the following set of commands in a shell script
modprobe nbd
sudo qemu-nbd -c /dev/nbd0 path/to/image/file
sudo mount /dev/nbd0p1 /mnt/temp
python copyFiles.py
sudo umount /mnt/temp
sudo qemu-nbd -d /dev/nbd0
sudo rmmod nbd
When I individually run these commands it works fine, but when I put them in a shell script and executed that shell script, I always end up with an error in the mount command.
So I threw in a sleep 1 before mount and it works as expected.
What could be the reason behind this?
(Some sort of asynchronous call registration delay/ race condition?)
mount error: mount point /mnt/temp does not exist
So it seems the directory /mnt/temp doesn't exist when you are running it as a shell script. Just create it or add this in your script somewhere before the mount command:
mkdir /mnt/temp 2>&1 /dev/null
Both mount and the previous command require escalated privileges. Does it error cause the lock is still in place from the previous command when mount tries to run?

Resources