Reputation: 11091
Is there any way how I can run two Db2 commands from a command line? They will be called from a PHP exec
command.
db2 connect to ttt
(note that we need to have the connection live for the second commanddb2 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.
Upvotes: 185
Views: 154812
Reputation: 6476
This is a clean solution that works well for multiple lines
sudo bash -c """
id
echo "testing"
"""
Upvotes: 2
Reputation: 3307
For your command you also could refer to the following example:
sudo sh -c 'whoami; whoami'
Upvotes: 208
Reputation: 541
If you know the root password, you can try
su -c "<command1> ; <command2>"
Upvotes: 1
Reputation: 1706
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.
Upvotes: 2
Reputation: 4503
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
Upvotes: 2
Reputation: 6646
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
Upvotes: 15
Reputation: 7755
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.
Upvotes: 173
Reputation: 39
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;'
Upvotes: 3
Reputation: 491
If you would like to handle quotes:
sudo -s -- <<EOF
id
pwd
echo "Done."
EOF
Upvotes: 49
Reputation: 4265
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.
Upvotes: 0