171

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 = 'mytestaccount@gmail.com'

I tried this:

sudo -su db2inst1 db2 connect to ttt; db2 UPDATE CONTACT SET EMAIL_ADDRESS = 'mytestaccount@gmail.com'

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.

2
  • please leave a comment why you want to close this question. Thank you.
    – Radek
    Apr 6, 2011 at 1:19
  • 1
    The close vote is for migration to serverfault, since this is a systems administration question, not programming.
    – bdonlan
    Apr 6, 2011 at 1:23

11 Answers 11

195

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

sudo sh -c 'whoami; whoami'

1
  • 14
    I have found this a more reliable alternative.
    – Nick
    Jun 14, 2012 at 14:58
166

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 = 'mytestaccount@gmail.com'"

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.

12
  • 2
    it gives me /bin/bash: db2: command not found /bin/bash: db2: command not found if running as php user
    – Radek
    Apr 6, 2011 at 1:29
  • 14
    this doesn't work on latest debian stable (squeeze) bash: sudo -s -- '/usr/bin/whoami; /usr/bin/whoami' /bin/bash: /usr/bin/whoami; /usr/bin/whoami: No such file or directory
    – Valor
    Nov 2, 2012 at 11:48
  • 13
    @Valor you can use sudo -- sh -c 'whoami; whoami; as a workaround when "sudo -s" is broken. I've updated the answer as well.
    – wjl
    Nov 3, 2012 at 15:19
  • 6
    Can you please explain why you need "--" ? May 15, 2016 at 9:24
  • 9
    @VicSeedoubleyew The -- indicates the end of parameters to sudo, so everything else is part of the command; without it, arguments like -c could be interpreted as an argument to sudo. This works for most (but not all) command-line programs. For another (non-sudo) example, to remove a file called -f you can't just run rm -f, right?! But you can run rm -- -f to delete the file called -f.
    – wjl
    May 15, 2016 at 16:00
49

If you would like to handle quotes:

sudo -s -- <<EOF
id
pwd
echo "Done."
EOF
1
  • 6
    This is the cleanest answer here, thanks. Maybe add a note "You can't run multiple commands from sudo - you always need to trick it into executing a shell which may accept multiple commands to run as parameters"
    – trs
    Dec 3, 2017 at 20:47
46

I usually do:

sudo bash -c 'whoami; whoami'
14

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
1
  • Thank you! Even the subshell didn't work for me. Feb 26 at 7:14
3

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;'
3

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
1

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.

1

If you know the root password, you can try

su -c "<command1> ; <command2>"  
1
  • 1
    If you don't know the root password, use sudo su -c "<command1> ; <command2>" . Apr 22, 2020 at 19:14
0

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.

2
  • You can use single and double quotes and escape them.
    – Radek
    Sep 14, 2012 at 23:34
  • Ok, so how come,now matter what I set the umask to in this command, it has no effect?
    – Michael
    Sep 15, 2013 at 23:51
0

This is a clean solution that works well for multiple lines

sudo bash -c """
id
echo "testing"
"""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.