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 &&
:
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
).
I even did this manually and from the Jenkins machine logged into the other server (servername). Then ran sh -c "sudo su && df"
with no luck.
Can you please help?
Thanks in advance
If you are trying to run the df
command as root, you should instead do sudo df
.
This is because with sudo su && df
, you are basically executing sudo su
first and then df
.
Also make sure, your jenkins user can be sudo without password.
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. 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. 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.
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.