如何使用 sudo 运行两个命令?
有什么方法可以从命令行运行两个 Db2 命令吗?它们将从 PHP exec
命令中调用。
db2 连接到 ttt
(请注意,我们需要为第二个命令建立连接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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对于您的命令,您还可以参考以下示例:
sudo sh -c 'whoami; whoami'
For your command you also could refer to the following example:
sudo sh -c 'whoami; whoami'
sudo 可以通过 shell 运行多个命令,例如:
您的命令将类似于:
如果您的 sudo 版本不支持带 -s 的分号(显然,如果使用某些选项编译则不支持),您可以
使用,它基本上做同样的事情,但让你明确命名 shell。
sudo can run multiple commands via a shell, for example:
Your command would be something like:
If your sudo version doesn't work with semicolons with -s (apparently, it doesn't if compiled with certain options), you can use
instead, which basically does the same thing but makes you name the shell explicitly.
如果您想处理报价:
If you would like to handle quotes:
我通常这样做:
I usually do:
使用
eval
的替代方案,以避免使用子 shell:注意:使用
sudo -s
的其他答案失败,因为引号被传递到 bash 并运行为单个命令,因此需要用 eval 去掉引号。eval
更好的解释是这个 SO 答案在命令中引用是也更容易:
如果命令在失败时需要停止运行,请使用双与号而不是分号:
An alternative using
eval
so avoiding use of a subshell: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 answerQuoting within the commands is easier too:
And if the commands need to stop running if one fails use double-ampersands instead of semi-colons:
-s
选项对我不起作用,-i
却对我有用。以下是如何从 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:
在终端上,键入:
然后编写任意数量的命令。完成后输入
exit
。如果您需要自动化它,请创建一个 script.sh 文件并运行它:
On the terminal, type:
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:在一个稍微相关的主题上,我想通过 SSH 执行相同的多命令 sudo,但以上方法都不起作用。
例如在 Ubuntu 上,
技巧 这里发现是双引号命令。
其他也有效的选项:
原则上,需要双引号,因为我认为运行 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,
The trick discovered here is to double-quote the command.
Other options that also work:
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.
如果你知道root密码,可以尝试
If you know the root password, you can try
这是一个干净的解决方案,适用于多条生产线
This is a clean solution that works well for multiple lines
上面的答案不允许您在引号内引用。该解决方案将:
umask 命令和 mkdir 命令都以“nobody”用户运行。
The above answers won't let you quote inside the quotes. This solution will:
Both the umask command and the mkdir-command runs in with the 'nobody' user.