如何使用 sudo 运行两个命令?

发布于 2024-10-31 05:45:04 字数 917 浏览 4 评论 0原文

有什么方法可以从命令行运行两个 Db2 命令吗?它们将从 PHP exec 命令中调用。

  1. db2 连接到 ttt (请注意,我们需要为第二个命令建立连接
  2. db2 UPDATE CONTACT SET EMAIL_ADDRESS = '[email protected]'

我尝试了以下操作:

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

第一个命令正确完成,但第二个命令失败并显示以下错误消息:

SQL1024N 数据库连接不存在。 SQLSTATE=08003

请注意,我需要以 php 用户身份运行它。作为 php 用户的命令 sudo -u db2inst1 id 为我提供了正确的输出。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

滥情哥ㄟ 2024-11-07 05:45:04

对于您的命令,您还可以参考以下示例:

sudo sh -c 'whoami; whoami'

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

sudo sh -c 'whoami; whoami'

许仙没带伞 2024-11-07 05:45:04

sudo 可以通过 shell 运行多个命令,例如:

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

您的命令将类似于:

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

如果您的 sudo 版本不支持带 -s 的分号(显然,如果使用某些选项编译则不支持),您可以

sudo -- sh -c 'whoami; whoami'

使用,它基本上做同样的事情,但让你明确命名 shell。

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.

浮云落日 2024-11-07 05:45:04

如果您想处理报价:

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

If you would like to handle quotes:

sudo -s -- <<EOF
id
pwd
echo "Done."
EOF
疯了 2024-11-07 05:45:04

我通常这样做:

sudo bash -c 'whoami; whoami'

I usually do:

sudo bash -c 'whoami; whoami'
伴随着你 2024-11-07 05:45:04

使用 eval 的替代方案,以避免使用子 shell:

sudo -s eval 'whoami; whoami'

注意:使用 sudo -s 的其他答案失败,因为引号被传递到 bash 并运行为单个命令,因此需要用 eval 去掉引号。 eval 更好的解释是这个 SO 答案

在命令中引用是也更容易:

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

如果命令在失败时需要停止运行,请使用双与号而不是分号:

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

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
荒路情人 2024-11-07 05:45:04

-s 选项对我不起作用,-i 却对我有用。

以下是如何从 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;'

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;'
找回味觉 2024-11-07 05:45:04

在终端上,键入:

$ sudo bash

然后编写任意数量的命令。完成后输入 exit

如果您需要自动化它,请创建一个 script.sh 文件并运行它:

$ sudo ./script.sh

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
花开柳相依 2024-11-07 05:45:04

在一个稍微相关的主题上,我想通过 SSH 执行相同的多命令 sudo,但以上方法都不起作用。

例如在 Ubuntu 上,

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

技巧 这里发现是双引号命令。

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

其他也有效的选项:

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

原则上,需要双引号,因为我认为运行 SSH 的客户端 shell 会去掉最外面的一组引号。根据您的需要混合并匹配引号(例如,需要传入变量)。但是,YMMV 带引号,尤其是在远程命令很复杂的情况下。在这种情况下,像 Ansible 这样的工具会是更好的选择。

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.

谢绝鈎搭 2024-11-07 05:45:04

如果你知道root密码,可以尝试

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

If you know the root password, you can try

su -c "<command1> ; <command2>"  
顾挽 2024-11-07 05:45:04

这是一个干净的解决方案,适用于多条生产线

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

This is a clean solution that works well for multiple lines

sudo bash -c """
id
echo "testing"
"""
未央 2024-11-07 05:45:04

上面的答案不允许您在引号内引用。该解决方案将:

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

umask 命令和 mkdir 命令都以“nobody”用户运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文