How can I execute both commands in an sh file? - shell

I have created an sh file:
#!/bin/bash
sudo python3 /home/pi/Desktop/try.py
sudo matchbox-keyboard
and when I execute the file, the program: try.py runs but the matchbox keyboard does not appear. Only when I close the program will the matchbox keyboard will appear. I want them to both appear at the same time.

The script executes both commands sequentially and waits each time until the command returns.
You could change it to:
#!/bin/bash
sudo python3 /home/pi/Desktop/try.py &
sudo matchbox-keyboard

Related

How could I run a shell script with delay

I basically want to run a script which is a server but with 10 second delay, it is because I need some stuff to run before this script.
The server is located in the folder /etc/init.d but basically to make it work I go to that path using the command line and I have to restart the server typing:
sudo ./znodejs.sh stop
And then I start the server again:
sudo ./znodejs.sh start
I would like to know if there is any way to run those commands with a delay.
In order to make a script run on startup first make it executable:
$ sudo chmod 755 /etc/init.d/znodejs.sh
Then you can register the script to be run at startup:
$ sudo update-rc.d znodejs.sh defaults
(Edit)
original answer:
the sleep command sill pause for a given number of seconds:
sudo ./znodejs.sh stop
sleep 10
sudo ./znodejs.sh start
The standard unix command for sleeping is called
sleep
to wait a second, use
sleep 1

Bash dropping out of sudo in a script

I need to execute an install script using sudo, but towards the end of the script, the script needs to drop out of sudo and continue as the regular user.
Example:
sudo ./install.sh
script runs and does what it needs to as root
su myscriptuser
service myscript start
Basically, the service myscript start needs to be run by the regular user, not by root.
su myscriptuser starts another shell in the name of myscriptuser and waits until it exits. Then it proceeds to run service myscript start in the name of root again.
What you need instead of the last 2 commands is sudo:
sudo -u myscriptuser service myscript start

Execute multiple command onto same terminal using bash script

I am trying to run a bash script which contains few commands to execute. I want to open a terminal and execute multiple commands into it. I have written commands to set the directory path and want to make a folder there.
Code :
gnome-terminal --working-directory=/var/run/
gnome-terminal -e "bash -c \"sudo mkdir sphinxsearch; exec bash\""
Here, There are 2 problems :
1) Two separate terminal are opened that I don't want. I need only a single terimal where I will execute my commands.
2) sudo mkdir sphinxsearch folder is created at the default path from where I am executing my bash script. I need to create a folder inside /var/run/
Each invocation of gnome-terminal will open a separate terminals.
Try this:
gnome-terminal --working-directory=/var/run/ -e "bash -c \"sudo mkdir sphinxsearch; exec bash\""
Here i am combining both options in a single invocation of gnome-terminal
sudo mkdir /var/run/sphinxsearch;
will create the folder in /var/run/

Run a command as the standard user, from a sudo elevated script

If a bash script has been executed with sudo, how can a command within the script run as the currently logged in user, instead of root and then revert to root to continue running other commands?
For example: -
#!/usr/bash
touch fileOwnedByRoot.txt
touch fileOwnedByUser.txt
touch otherRootFile.txt
If this script is run with sudo, without changing the order of commands, how can the 2nd touch command be run as the standard user?
The script is only a simple example, so using chmod to change ownership of files created is irrelevant.
The actual script I'm using is being run by an installer, so running with elevated privileges is a requirement, but specific commands must be run as the user running the installer, whose name is not known.
Use su - another_user -c "<command>" to run that specific command:
#!/bin/bash
touch /tmp/f1
su - another_user -c "touch /tmp/f2"
touch /tmp/f3
As commented by chepner below, you need to use $SUDO_USER or $SUDO_UID to get the name of the real user running the sudo command:
su - $SUDO_USER -c "touch /tmp/f2"
This way, the file will be touched by the user running the command.
You can test with:
#!/bin/bash
echo "sudo_user: $SUDO_USER"
echo "sudo_uid: $SUDO_UID"
And run the script either with ./script or sudo ./script. In the second case the values will be populated.
Don't run the script as sudo, just the commands that require elevated privileges.
#!/bin/bash
sudo touch fileOwnedByRoot.txt
touch fileOwnedByUser.txt
sudo touch otherRootFile.txt
According to the man page the environment variable SUDO_USER is set when you run sudo, so you could do something like:
#!/usr/bash
touch fileOwnedByRoot.txt
sudo ${SUDO_USER} touch fileOwnedByUser.txt
touch otherRootFile.txt
I haven't tested this, and don't know if it work differently on OSX, but it's worth a shot.

Execute single command in shell script without sudo

I have a simple shell script that is run with sudo as most of the script requires it, yet one of the commands in the script is a Homebrew install, which cannot be executed with sudo..
So, my question is when executing a shell script with sudo how do I execute sub commands as the current user and then continue the remainder of the script with sudo.
Prompting the user to enter his password again is not really practical as the script takes really long to execute and would require waiting 5-10 min for the prompt.
The easiest way is to run the subcommand via sudo from within the script. The user id to run with can be obtained by $SUDO_USER (look at the output of sudo env):
sudo -u $SUDO_USER ./exec_as_normal_user.sh
Instantiate the shell using
sudo -u $USER_NAME bash
and execute the shell script by calling,
./program.sh

Resources