How to run two commands with sudo?

BashSudo

Bash Problem Overview


Is there any way how I can run two Db2 commands from a command line? They will be called from a PHP exec command.

  1. db2 connect to ttt (note that we need to have the connection live for the second command
  2. db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'

I tried this:

sudo -su db2inst1 db2 connect to ttt; db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'

The first command finishes correctly, but the second one fails with the following error message: > SQL1024N A database connection does not exist. SQLSTATE=08003

Note that I need to run this as php user. The command sudo -u db2inst1 id as php user gives me correct output.

Bash Solutions


Solution 1 - Bash

For your command you also could refer to the following example:

sudo sh -c 'whoami; whoami'

Solution 2 - Bash

sudo can run multiple commands via a shell, for example:

$ sudo -s -- 'whoami; whoami'
root
root

Your command would be something like:

sudo -u db2inst1 -s -- "db2 connect to ttt; db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'"

If your sudo version doesn't work with semicolons with -s (apparently, it doesn't if compiled with certain options), you can use

sudo -- sh -c 'whoami; whoami'
instead, which basically does the same thing but makes you name the shell explicitly.

Solution 3 - Bash

If you would like to handle quotes:

sudo -s -- <<EOF
id
pwd
echo "Done."
EOF

Solution 4 - Bash

I usually do:

sudo bash -c 'whoami; whoami'

Solution 5 - Bash

An alternative using eval so avoiding use of a subshell:

sudo -s eval 'whoami; whoami'

Note: The other answers using sudo -s fail because the quotes are being passed on to bash and run as a single command so need to strip quotes with eval. eval is better explained is this SO answer

Quoting within the commands is easier too:

$ sudo -s eval 'whoami; whoami; echo "end;"'
root
root
end;

And if the commands need to stop running if one fails use double-ampersands instead of semi-colons:

$ sudo -s eval 'whoami && whoamit && echo "end;"'
root
/bin/bash: whoamit: command not found

Solution 6 - Bash

The -s option didn't work for me, -i did.

Here is an example of how I could update the log size from my bash:

sudo -u [user] -i -- sh -c 'db2 connect to [database name];db2 update db cfg for [database name] using logsecond 20;db2 update db cfg for [database name] using logprimary 20;'

Solution 7 - Bash

On the terminal, type:

$ sudo bash

Then write as many commands as you want. Type exit when you done.

If you need to automate it, create a script.sh file and run it:

$ sudo ./script.sh

Solution 8 - Bash

On a slightly-related topic, I wanted to do the same multi-command sudo via SSH but none of the above worked.

For example on Ubuntu,

$ ssh host.name sudo sh -c "whoami; whoami"
[sudo] password for ubuntu:
root
ubuntu

The trick discovered here is to double-quote the command.

$ ssh host.name sudo sh -c '"whoami; whoami"'
[sudo] password for ubuntu:
root
root

Other options that also work:

ssh host.name sudo sh -c "\"whoami; whoami\""
ssh host.name 'sudo sh -c "whoami; whoami"'

In principle, double-quotes are needed because I think the client shell where SSH is run strips the outermost set of quotes. Mix and match the quotes to your needs (eg. variables need to be passed in). However YMMV with the quotes especially if the remote commands are complex. In that case, a tool like Ansible will make a better choice.

Solution 9 - Bash

If you know the root password, you can try

su -c "<command1> ; <command2>"  

Solution 10 - Bash

The above answers won't let you quote inside the quotes. This solution will:

sudo -su nobody umask 0000 \; mkdir -p "$targetdir"

Both the umask command and the mkdir-command runs in with the 'nobody' user.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRadekView Question on Stackoverflow
Solution 1 - BashJasonView Answer on Stackoverflow
Solution 2 - BashwjlView Answer on Stackoverflow
Solution 3 - BashNebojšaView Answer on Stackoverflow
Solution 4 - BashSamer AtianiView Answer on Stackoverflow
Solution 5 - BashCasView Answer on Stackoverflow
Solution 6 - BashabhihomeView Answer on Stackoverflow
Solution 7 - BashMoshe SimantovView Answer on Stackoverflow
Solution 8 - BashEugene ChowView Answer on Stackoverflow
Solution 9 - BashnomadView Answer on Stackoverflow
Solution 10 - BasharbergView Answer on Stackoverflow