简体   繁体   English

“ sudo su && somecommand”未运行somecommand

[英]“sudo su && somecommand” doesn't run somecommand

I have created a Jenkins job today, what it does is the Jenkins user should log into another server and run two commands seperated by && : 我今天创建了一个Jenkins作业,它的作用是Jenkins用户应登录到另一台服务器并运行由&&分隔的两个命令:

ssh -i /creds/jenkins jenkins@servername.com "sh -c 'sudo su && df'"

The loging part works fine, then it runs the sudo su command and becomes root but it never runs the second command (ie df ). 日志记录部分工作正常,然后运行sudo su命令并成为root用户,但从不运行第二个命令(即df )。

I even did this manually and from the Jenkins machine logged into the other server (servername). 我什至手动执行此操作,并从Jenkins机器登录到另一台服务器(服务器名)。 Then ran sh -c "sudo su && df" with no luck. 然后没有运气就跑了sh -c "sudo su && df"

Can you please help? 你能帮忙吗?

Thanks in advance 提前致谢

If you are trying to run the df command as root, you should instead do sudo df . 如果您尝试以root用户身份运行df命令,则应改为sudo df

This is because with sudo su && df , you are basically executing sudo su first and then df . 这是因为使用sudo su && df基本上是先执行sudo su ,然后执行df

Also make sure, your jenkins user can be sudo without password. 还请确保您的jenkins用户无需密码即可使用sudo。

sudo su启动第二个shell, && df sudo su shell成功退出之后,包含&& df的命令正等待在非root用户shell中执行。

This could be what you're looking for: 这可能是您要寻找的:

sh -c 'sudo su - root -c "df"'

Edit: please note that I don't normally use or advocate the use of sudo su - root -c type of constructions. 编辑:请注意,我通常不使用或不提倡使用sudo su - root -c类型的构造。 However, I have seen rare cases in which a program doesn't work properly when called via sudo/gksudo , but does work properly when called via su/gksu -- in such cases, a given user should try to use sudo -i first , and if that does not work, one might have to resort to sudo su - root -c or similar, as a workaround of sorts to deal with a "misbehaving" program. 但是,我见过极少数情况,其中通过sudo/gksudo调用时程序无法正常sudo/gksudo ,而通过su/gksu调用时程序却可以正常运行-在这种情况下,给定的用户应首先尝试使用sudo -i ,如果这不起作用,则可能必须求助于sudo su - root -c或类似的解决方案,以处理“行为异常”程序。 Since the OP used some similar syntax on his post, I assumed that his case could be such a workaround case, so I maintained the sudo su - root -c type of structure on my answer. 由于OP在他的帖子中使用了类似的语法,因此我认为他的情况可能是一种变通的情况,因此我在回答中保留了sudo su - root -c类型的结构。

when you did sudo su && df , sudo su will start a child process immediately without waiting for the && df part of the command to execute , when you hit Ctrl + D it exits the child process and enters the parent shell , that's when your && df will execute. 当您执行sudo su && dfsudo su将立即启动子进程,而无需等待命令的&& df部分执行,当您Ctrl + D它将退出子进程并进入父外壳,此时您的&& df将执行。 You should do this using here strings, it might not be the best option but it works and it does not start a new child process 您应该使用此处的字符串来执行此操作,它可能不是最好的选择,但它可以工作并且不会启动新的子进程

sh -c "sudo su" <<<df

note: don't surround <<< df with any quotes 注意:请勿在<<< df前后加上任何引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM